Welcome 微信登录

首页 / 网页编程 / ASP.NET / LINQ体验(14)——LINQ to SQL语句之存储过程

LINQ体验(14)——LINQ to SQL语句之存储过程2010-11-19 cnblogs lyj存储过程

在我们编写程序中,往往需要一些存储过程,在LINQ to SQL中 怎么使用呢?也许比原来的更简单些。下面我们以NORTHWND.MDF中自带的 几个存储过程来理解一下。

1.标量返回

在数据库中,有名为 Customers Count By Region的存储过程。该存储过程返回顾客所在 "WA"区域的数量。

ALTER PROCEDURE [dbo]. [NonRowset]
(@param1 NVARCHAR(15))
AS
BEGIN
SET NOCOUNT ON;
DECLARE @count int
SELECT @count = COUNT(*)FROM Customers
WHERECustomers.Region = @Param1
RETURN @count
END

我们只要把这个存储过程拖到O/R设 计器内,它自动生成了以下代码段:

[Function(Name = "dbo.[Customers Count By Region]")]
public int Customers_Count_By_Region([Parameter
(DbType = "NVarChar (15)")] string param1)
{
IExecuteResult result = this.ExecuteMethodCall(this,
((MethodInfo) (MethodInfo.GetCurrentMethod())), param1);
return ((int) (result.ReturnValue));
}

我们需要时,直接调用就可以了, 例如:

int count = db.CustomersCountByRegion ("WA");
Console.WriteLine(count);

语句描述:这 个实例使用存储过程返回在“WA”地区的客户数。