在ASP.NET 2.0中操作数据之七十三:用Managed Code创建存储过程
更新GetProductsWithPriceLessThan方法以使其接收一个名叫price的SqlMoney类型的输入参数.代码如下: [Microsoft.SqlServer.Server.SqlProcedure] public static void GetProductsWithPriceLessThan(SqlMoney price) { // Create the command SqlCommand myCommand = new SqlCommand(); myCommand.CommandText = @"SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued FROM Products WHERE UnitPrice < @MaxPrice"; myCommand.Parameters.AddWithValue("@MaxPrice", price); // Execute the command and send back the results SqlContext.Pipe.ExecuteAndSend(myCommand); } GetProductsWithPriceLessThan方法的生命代码和我们在第三步创建的GetDiscontinuedProducts方法的声明代码很相似.唯一的不同在于GetProductsWithPriceLessThan方法接收一个输入参数(price), 且SqlCommand的查询里也包含了一个参数(@MaxPrice). 完成代码添加后,重新部署SQL Server Project.接下来,返回到SQL Server Management Studio并刷新Stored Procedures文件夹.你将会看到一个新的查询——GetProductsWithPriceLessThan.从查询窗口,键入并执行命令“exec GetProductsWithPriceLessThan 25”,它会将所有价格低于25的产品显示出来,如图14所示.
第六步:从数据访问层调用Managed Stored Procedure 此时,我们已经向ManagedDatabaseConstructs工程添加了GetDiscontinuedProducts 和 GetProductsWithPriceLessThan这2个managed stored procedures,并把它们注册到Northwind SQL Server数据库.我们同样从SQL Server Management调用它们(见图13和14)。为了使我们的ASP.NET应用程序能调用这些managed stored procedures,因此我们需要将它们添加到体系的数据访问层和业务逻辑层.在这一步,我们向类型化数据集NorthwindWithSprocs的ProductsTableAdapter添加2个新的方法.在第七步,我们将添加相应的方法到业务逻辑层. 在Visual Studio里打开类型化数据集NorthwindWithSprocs,向ProductsTableAdapter添加一个名为GetDiscontinuedProducts的方法. 注意:由于我们已经将Northwind数据库移出了App_Data文件夹,所以我们应该在Web.config文件里对连接字符串进行相应的更新。在第二步我们探讨了更新Web.config文件里的NORTHWNDConnectionString的值.如果你忘记了更新的话,当你试图向TableAdapter添加新方法的时候,你将看到这样的错误信息:“Failed to add query. Unable to find connection ‘NORTHWNDConnectionString' for object ‘Web.config'”.要克服这个问题, 需要更新Web.config文件的NORTHWNDConnectionString值.就像在第二步探讨的那样,再重新向TableAdapter添加新方法,这次就不会出错了. 添加新方法的时候,TableAdapter查询配置向导首先询问我们如何访问数据库。由于我们已经创建并注册了GetDiscontinuedProducts存储过程,因此选“Use existing stored procedure”项,点Next.
接下来要我们选要调用的存储过程。在左边下拉列表里选存储过程GetDiscontinuedProducts.
由于GetDiscontinuedProducts将返回一系列的记录行,我们选第一项(“Tabular data”) ,再点Next.
采用同样的方法,为名为GetProductsWithPriceLessThan的managed stored procedure在ProductsTableAdapter里添加FillByPriceLessThan和 GetProductsWithPriceLessThan这2个方法. 图19显示的是为GetDiscontinuedProducts 和 GetProductsWithPriceLessThan这2个managed stored procedures在ProductsTableAdapter里添加各种方法后的界面.
第七步:向业务逻辑层添加相应的方法 现在我们已经更新了数据访问层,我们还需要向业务逻辑层添加相应的方法.向ProductsBLLWithSprocs class类添加如下的2种方法: [System.ComponentModel.DataObjectMethodAttribute (System.ComponentModel.DataObjectMethodType.Select, false)] public NorthwindWithSprocs.ProductsDataTable GetDiscontinuedProducts() { return Adapter.GetDiscontinuedProducts(); } [System.ComponentModel.DataObjectMethodAttribute (System.ComponentModel.DataObjectMethodType.Select, false)] public NorthwindWithSprocs.ProductsDataTable GetProductsWithPriceLessThan(decimal priceLessThan) { return Adapter.GetProductsWithPriceLessThan(priceLessThan); } (编辑:源码网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |