接下来要我们为TableAdapter的方法命名并指定用于访问和更新数据的模式.我们全部选中这3项,不过将GetData方法重命名为GetSuppliers.点击Finish完成配置.
图5:将GetData方法重命名为GetSuppliers
完成后向导将创建这4个存储过程,并向类型化的DataSet添加ableAdapter以及对应的DataTable.
第四步:在TableAdapter的主查询里引用Computed Column列
接下来我们将对第三步创建的TableAdapter 和 DataTable进行更新以引用FullContactName列,这要经过2个步骤:
1.更新名为Suppliers_Select的存储过程以返回FullContactName列
2.更新DataTable以包含相应的FullContactName列
首先在服务器资源管理器里打开存储过程文件夹,打开Suppliers_Select存储过程,更新其SELECT查询以引用FullContactName列:
SELECT SupplierID, CompanyName, ContactName, ContactTitle, FullContactName FROM Suppliers
保存所做的修改.接下来返回到DataSet Designer,在SuppliersTableAdapter上右键单击,选“Configure”.我们可以注意到Suppliers_Select里的Data Columns集里已经包含了FullContactName列.
图6:返回到TableAdapter的设置向导更新DataTable的列
点击Finish完成设置,这将自动地为SuppliersDataTable添加相应的列.TableAdapter发觉FullContactName列是一个computed column列,且是只读的.因此将设置该列的ReadOnly属性为true.我们可以进行验证:在SuppliersDataTable里选择该列,打开其属性窗口(如图7),我们注意到FullContactName列的DataType 和 MaxLength属性都作了相应的设置.
图7:FullContactName列标记为Read-Only
第五步:向TableAdapter添加一个GetSupplierBySupplierID方法
在本文我们将在一个具有更新功能的ASP.NET页面里展示suppliers信息.在前面的文章里,我们从DAL获取指定的记录并将其作为一个强类型的DataTable返回给BLL以做更新,然后将更新后的DataTable再传递给DAL,对数据库做相应的改动.为此,第一步——从DAL返回要更新的记录——我们需要向DAL层添加一个名为GetSupplierBySupplierID(supplierID)的方法.
在DataSet Design设计器里右键单击SuppliersTableAdapter,选“Add Query” ,再选“Create new stored procedure”(可参考3图)。再选“SELECT which returns rows”再点Next.
图8:选“SELECT which returns rows”项
接下来为该方法指定查询,键入如下的代码,它将检索某个具体的supplier返回的列与主查询一样.
SELECT SupplierID, CompanyName, ContactName, ContactTitle, FullContactName FROM Suppliers WHERE SupplierID = @SupplierID
接下来我们将该存储过程命名为Suppliers_SelectBySupplierID,点Next.
图9:将存储过程命名为Suppliers_SelectBySupplierID
在接下来的界面,全部选中图里的2项,并将FillBy 和 GetDataBy方法分别命名为FillBySupplierID 和 GetSupplierBySupplierID.
图10:将TableAdapter的方法命名为FillBySupplierID 和 GetSupplierBySupplierID
点Finish完成向导
第六步:创建业务逻辑层Business Logic Layer
在创建ASP.NET页面前,我们首先要在BLL添加相应的方法.我们将在第7步创建页面,其允许我们查看并编辑suppliers.因此我们在BLL至少要包含2个方法,一个获取所有的suppliers,一个用于更新某个具体的supplier.
在~/App_Code/BLL文件夹里创建一个名为SuppliersBLLWithSprocs的新类,添加代码如下:
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using NorthwindWithSprocsTableAdapters; [System.ComponentModel.DataObject] public class SuppliersBLLWithSprocs { private SuppliersTableAdapter _suppliersAdapter = null; protected SuppliersTableAdapter Adapter { get { if (_suppliersAdapter == null) _suppliersAdapter = new SuppliersTableAdapter(); return _suppliersAdapter; } } [System.ComponentModel.DataObjectMethodAttribute (System.ComponentModel.DataObjectMethodType.Select, true)] public NorthwindWithSprocs.SuppliersDataTable GetSuppliers() { return Adapter.GetSuppliers(); } [System.ComponentModel.DataObjectMethodAttribute (System.ComponentModel.DataObjectMethodType.Update, true)] public bool UpdateSupplier(string companyName, string contactName, string contactTitle, int supplierID) { NorthwindWithSprocs.SuppliersDataTable suppliers = Adapter.GetSupplierBySupplierID(supplierID); if (suppliers.Count == 0) // no matching record found, return false return false; NorthwindWithSprocs.SuppliersRow supplier = suppliers[0]; supplier.CompanyName = companyName; if (contactName == null) supplier.SetContactNameNull(); else supplier.ContactName = contactName; if (contactTitle == null) supplier.SetContactTitleNull(); else supplier.ContactTitle = contactTitle; // Update the product record int rowsAffected = Adapter.Update(supplier); // Return true if precisely one row was updated, otherwise false return rowsAffected == 1; } }