第八步:从表现层调用Managed Stored Procedures
当对数据访问层和业务逻辑层进行扩充以支持调用GetDiscontinuedProducts 和 GetProductsWithPriceLessThan这2种managed stored procedures后,我们可以在一个ASP.NET页面里展示这些存储过程的结果了.
打开AdvancedDAL文件夹里的ManagedFunctionsAndSprocs.aspx页面,从工具箱拖一个GridView控件到设计器,设其ID为DiscontinuedProducts,在其智能标签里绑定到一个名为DiscontinuedProductsDataSource的ObjectDataSource控件,设置其调用ProductsBLLWithSprocs class类的GetDiscontinuedProducts方法.
图20:调用ProductsBLLWithSprocs Class类
图21:在SELECT标签里调用GetDiscontinuedProducts方法
由于我们只需要展示产品信息,在UPDATE, INSERT,和DELETE标签里选 “(None)”,再点Finish完成配置.完成后Visual Studio会为ProductsDataTable表的列自动的添加BoundField列 或 CheckBoxField列. 将除ProductName和Discontinued以外的列全部删除.这样你的GridView 和 ObjectDataSource的声明代码看起来和下面的差不多:
<asp:GridView ID="DiscontinuedProducts" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="DiscontinuedProductsDataSource"> <Columns> <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" /> <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued" /> </Columns> </asp:GridView> <asp:ObjectDataSource ID="DiscontinuedProductsDataSource" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetDiscontinuedProducts" TypeName="ProductsBLLWithSprocs"> </asp:ObjectDataSource></div>
花点时间在浏览器里登录该页面。当登录时,ObjectDataSource控件将调用ProductsBLLWithSprocs class类的 GetDiscontinuedProducts方法.就像我们在第七步看到的那样,该方法又调用DAL层的ProductsDataTable class类的GetDiscontinuedProducts方法,该方法又调用存储过程GetDiscontinuedProducts.该存储过程返回那些处于“discontinued”状态的产品. 存储过程返回的结果填充到DAL层的一个ProductsDataTable,进而返回给BLL,再返回给表现层并绑定到一个GridView控件展现出来.
图22:“Discontinued”的产品被列出来了
我们可以继续加强练习,比如在页面上再放置一个TextBox控件和一个GridView控件。在TextBox控件里输入一个数,而GridView控件调用ProductsBLLWithSprocs class类的GetProductsWithPriceLessThan方法将价格低于该数的产品展示出来.
第九步:创建并调用T-SQL UDFs
用户自定义函数——简称UDF,是一种数据库对象,与编程语言里的函数定义很相仿.与C#里面的函数类似,UDF可以包含一系列的输入参数并返回一个特定类型的值.一个UDF要么返回标量数据(scalar data)——比如一个string, 一个integer等等;要么返回一个表列数据(tabular data).让我们先快速的考察一下这2种类型的UDF,先从标量数据类型开始.
下面的UDF用于计算某个特定产品的总价.其有3个输入参数——UnitPrice, UnitsInStock,Discontinued.其返回一个money类型的值.它通过以UnitPrice乘以UnitsInStock来得到总价,如是处于“discontinued”状态,则总价减半.
CREATE FUNCTION udf_ComputeInventoryValue ( @UnitPrice money, @UnitsInStock smallint, @Discontinued bit ) RETURNS money AS BEGIN DECLARE @Value decimal SET @Value = ISNULL(@UnitPrice, 0) * ISNULL(@UnitsInStock, 0) IF @Discontinued = 1 SET @Value = @Value * 0.5 RETURN @Value END</div>
将该UDF添加到数据库后,我们打开Management Studio,打开Programmability文件夹,再打开Functions文件夹,再打开Scalar-value Functions文件夹,就可以看到该UDF.我们可以在一个SELECT查询里这样来使用:
SELECT ProductID, ProductName, dbo.udf_ComputeInventoryValue (UnitPrice, UnitsInStock, Discontinued) as InventoryValue FROM Products ORDER BY InventoryValue DESC</div>
我已经将该udf_ComputeInventoryValue用户函数添加到了Northwind数据库。图23就是在Management Studio里调用上述SELECT查询得到的输出结果.
图23:列出了每个产品的总价
UDF也可以返回表列数据.比如,我们可以创建一个UDF返回属于某个category的所有产品:
CREATE FUNCTION dbo.udf_GetProductsByCategoryID ( @CategoryID int ) RETURNS TABLE AS RETURN ( SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued FROM Products WHERE CategoryID = @CategoryID )</div>
该udf_GetProductsByCategoryID用户函数接受一个@CategoryID输入参数,返回SELECT查询的结果.一旦创建之后,该UDF就可以在SELECT查询的FROM (或 JOIN)之句里引用.下面的示例返回饮料类所属的每个产品的ProductID, ProductName,CategoryID值:
SELECT ProductID, ProductName, CategoryID FROM dbo.udf_GetProductsByCategoryID(1)</div>
我已经将该udf_GetProductsByCategoryID用户函数添加到Northwind数据库。图24显示的是在Management Studio运行上述SELECT查询的结果.返回表列数据的UDF放在Table-value Functions文件夹里.
图24:饮料类产品的ProductID, ProductName,CategoryID都列出来了
注意:关于创建和使用UDF的更多详情,请参阅文章《Intro to User-Defined Functions》和《dvantages and Drawbacks of User-Defined Functions》
第十步:创建一个Managed UDF
上面示例里创建的udf_ComputeInventoryValue和 udf_GetProductsByCategoryID用户函数都是T-SQL数据库对象.SQL Server 2005同样支持managed UDF,我们可以将其添加到ManagedDatabaseConstructs工程,就像在第三和第五步做的那样.在这一步,我们将用managed code执行udf_ComputeInventoryValue用户函数.
在解决资源管理器里右键单击,选择“Add a New Item”,在对话框里选User-Defined Function模板,将新UDF文件命名为udf_ComputeInventoryValue_Managed.cs.
图25:向ManagedDatabaseConstructs工程添加一个Managed UDF
该User-Defined Function模板将创建一个名为UserDefinedFunctions的partial class类,同时还有一个方法,该方法的名字与类文件的名字一样(就本例而言,为udf_ComputeInventoryValue_Managed)。该方法有一个SqlFunction特性, 这就标明了该方法是一个managed UDF.
using System; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; public partial class UserDefinedFunctions { [Microsoft.SqlServer.Server.SqlFunction] public static SqlString udf_ComputeInventoryValue_Managed() { // Put your code here return new SqlString("Hello"); } }</div>
该udf_ComputeInventoryValue方法目前返回一个SqlString对象,且不接受任何的输入参数.我们将对其进行更新以包含3个参数——UnitPrice, UnitsIn