深度剖析LINQ to SQL存储过程

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

本文将从5个方面来学习LINQ to SQL存储过程,它们其中有LINQ to SQL存储过程之标量返回、LINQ to SQL存储过程之单一结果集等等。

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

1.LINQ to SQL存储过程之标量返回
在数据库中,有名为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  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

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));  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

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

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

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

2.LINQ to SQL存储过程之单一结果集
从数据库中返回行集合,并包含用于筛选结果的输入参数。 当我们执行返回行集合的存储过程时,会用到结果类,它存储从存储过程中返回的结果。

下面的示例表示一个存储过程,该存储过程返回客户行并使用输入参数来仅返回将“London”列为客户城市的那些行的固定几列。 

ALTER PROCEDURE [dbo].[Customers By City]  
     -- Add the parameters for the stored procedure here  
     (@param1 NVARCHAR(20))  
AS  
BEGIN  
     -- SET NOCOUNT ON added to prevent extra result sets from  
     -- interfering with SELECT statements.  
     SET NOCOUNT ON;  
     SELECT CustomerID, ContactName, CompanyName, City from   
     Customers as c where c.City=@param1 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

END拖到O/R设计器内,它自动生成了以下代码段:

[Function(Name="dbo.[Customers By City]")]  
public ISingleResult Customers_By_City(  
[Parameter(DbType="NVarChar(20)")] string param1)  
{  
    IExecuteResult result = this.ExecuteMethodCall(this, (  
    (MethodInfo)(MethodInfo.GetCurrentMethod())), param1);  
    return ((ISingleResult)  
    (result.ReturnValue));  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

我们用下面的代码调用:

ISingleResult result =  
 db.Customers_By_City("London");  
foreach (Customers_By_CityResult cust in result)  
{  
    Console.WriteLine("CustID={0}; City={1}", cust.CustomerID,  
        cust.City);  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

语句描述:这个实例使用存储过程返回在伦敦的客户的 CustomerID和City。

3.LINQ to SQL存储过程之多个可能形状的单一结果集

当存储过程可以返回多个结果形状时,返回类型无法强类型化为单个投影形状。尽管 LINQ to SQL 可以生成所有可能的投影类型,但它无法获知将以何种顺序返回它们。 ResultTypeAttribute 属性适用于返回多个结果类型的存储过程,用以指定该过程可以返回的类型的集合。

在下面的 SQL 代码示例中,结果形状取决于输入(param1 = 1或param1 = 2)。我们不知道先返回哪个投影。

ALTER PROCEDURE [dbo].[SingleRowset_MultiShape]  
     -- Add the parameters for the stored procedure here  
     (@param1 int )  
AS  
BEGIN  
     -- SET NOCOUNT ON added to prevent extra result sets from  
     -- interfering with SELECT statements.  
     SET NOCOUNT ON;  
     if(@param1 = 1)  
     SELECT * from Customers as c where c.Region = 'WA' 
     else if (@param1 = 2)  
     SELECT CustomerID, ContactName, CompanyName from   
     Customers as c where c.Region = 'WA' 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

END拖到O/R设计器内,它自动生成了以下代码段:

[Function(Name="dbo.[Whole Or Partial Customers Set]")]  
public ISingleResult   
Whole_Or_Partial_Customers_Set([Parameter(DbType="Int")]   
System.Nullable<int> param1)  
{  
    IExecuteResult result = this.ExecuteMethodCall(this,   
    ((MethodInfo)(MethodInfo.GetCurrentMethod())), param1);  
    return ((ISingleResult)  
    (result.ReturnValue));  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

但是,VS2008会把多结果集存储过程识别为单结果集的存储过程,默认生成的代码我们要手动修改一下,要求返回多个结果集,像这样:

[Function(Name="dbo.[Whole Or Partial Customers Set]")]  
[ResultType(typeof(WholeCustomersSetResult))]  
[ResultType(typeof(PartialCustomersSetResult))]  
public IMultipleResults Whole_Or_Partial_Customers_Set([Parameter  
(DbType="Int")] System.Nullable<int> param1)  
{  
    IExecuteResult result = this.ExecuteMethodCall(this,   
    ((MethodInfo)(MethodInfo.GetCurrentMethod())), param1);  
    return ((IMultipleResults)(result.ReturnValue));  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

我们分别定义了两个分部类,用于指定返回的类型。WholeCustomersSetResult类 如下:(点击展开)

 代码在这里展开

public partial class WholeCustomersSetResult  
{  
    private string _CustomerID;  
    private string _CompanyName;  
    private string _ContactName;  
    private string _ContactTitle;  
    private string _Address;  
    private string _City;  
    private string _Region;  
    private string _PostalCode;  
    private string _Country;  
    private string _Phone;  
    private string _Fax;  
    public WholeCustomersSetResult()  
    {  
    }  
    [Column(Storage = "_CustomerID", DbType = "NChar(5)")]  
    public string CustomerID  
    {  
        get { return this._CustomerID; }  
        set 
        {  
            if ((this._CustomerID != value))  
                this._CustomerID = value;  
        }  
    }  
    [Column(Storage = "_CompanyName", DbType = "NVarChar(40)")]  
    public string CompanyName  
    {  
        get { return this._CompanyName; }  
        set 
        {  
            if ((this._CompanyName != value))  
                this._CompanyName = value;  
        }  
    }  
    [Column(Storage = "_ContactName", DbType = "NVarChar(30)")]  
    public string ContactName  
    {  
        get { return this._ContactName; }  
        set 
        {  
            if ((this._ContactName != value))  
                this._ContactName = value;  
        }  
    }  
    [Column(Storage = "_ContactTitle", DbType = "NVarChar(30)")]  
    public string ContactTitle  
    {  
        get { return this._ContactTitle; }  
        set 
        {  
            if ((this._ContactTitle != value))  
                this._ContactTitle = value;  
        }  
    }  
    [Column(Storage = "_Address", DbType = "NVarChar(60)")]  
    public string Address  
    {  
        get { return this._Address; }  
        set 
        {  
            if ((this._Address != value))  
                this._Address = value;  
        }  
    }  
    [Column(Storage = "_City", DbType = "NVarChar(15)")]  
    public string City  
    {  
        get { return this._City; }  
        set 
        {  
            if ((this._City != value))  
                this._City = value;  
        }  
    }  
    [Column(Storage = "_Region", DbType = "NVarChar(15)")]  
    public string Region  
    {  
        get { return this._Region; }  
        set 
        {  
            if ((this._Region != value))  
                this._Region = value;  
        }  
    }  
    [Column(Storage = "_PostalCode", DbType = "NVarChar(10)")]  
    public string PostalCode  
    {  
        get { return this._PostalCode; }  
        set 
        {  
            if ((this._PostalCode != value))  
                this._PostalCode = value;  
        }  
    }  
    [Column(Storage = "_Country", DbType = "NVarChar(15)")]  
    public string Country  
    {  
        get { return this._Country; }  
        set 
        {  
            if ((this._Country != value))  
                this._Country = value;  
        }  
    }  
    [Column(Storage = "_Phone", DbType = "NVarChar(24)")]  
    public string Phone  
    {  
        get { return this._Phone; }  
        set 
        {  
            if ((this._Phone != value))  
                this._Phone = value;  
        }  
    }  
    [Column(Storage = "_Fax", DbType = "NVarChar(24)")]  
    public string Fax  
    {  
        get { return this._Fax; }  
        set 
        {  
            if ((this._Fax != value))  
                this._Fax = value;  
        }  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.

PartialCustomersSetResult类 如下:(点击展开)

代码在这里展开

public partial class PartialCustomersSetResult  
{  
    private string _CustomerID;  
    private string _ContactName;  
    private string _CompanyName;  
    public PartialCustomersSetResult()  
    {  
    }  
    [Column(Storage = "_CustomerID", DbType = "NChar(5)")]  
    public string CustomerID  
    {  
        get { return this._CustomerID; }  
        set 
        {  
            if ((this._CustomerID != value))  
                this._CustomerID = value;  
        }  
    }  
    [Column(Storage = "_ContactName", DbType = "NVarChar(30)")]  
    public string ContactName  
    {  
        get { return this._ContactName; }  
        set 
        {  
            if ((this._ContactName != value))  
                this._ContactName = value;  
        }  
    }  
    [Column(Storage = "_CompanyName", DbType = "NVarChar(40)")]  
    public string CompanyName  
    {  
        get { return this._CompanyName; }  
        set 
        {  
            if ((this._CompanyName != value))  
                this._CompanyName = value;  
        }  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.

这样就可以使用了,下面代码直接调用,分别返回各自的结果集合。

//返回全部Customer结果集  
IMultipleResults result = db.Whole_Or_Partial_Customers_Set(1);  
IEnumerable shape1 =  
 result.GetResult();  
foreach (WholeCustomersSetResult compName in shape1)  
{  
    Console.WriteLine(compName.CompanyName);  
}  
//返回部分Customer结果集  
result = db.Whole_Or_Partial_Customers_Set(2);  
IEnumerable shape2 =  
 result.GetResult();  
foreach (PartialCustomersSetResult con in shape2)  
{  
    Console.WriteLine(con.ContactName);  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

语句描述:这个实例使用存储过程返回“WA”地区中的一组客户。返回的结果集形状取决于传入的参数。如果参数等于 1,则返回所有客户属性。如果参数等于 2,则返回ContactName属性。

4.LINQ to SQL存储过程之多个结果集

这种存储过程可以生成多个结果形状,但我们已经知道结果的返回顺序。

下面是一个按顺序返回多个结果集的存储过程Get Customer And Orders。 返回顾客ID为"SEVES"的顾客和他们所有的订单。

ALTER PROCEDURE [dbo].[Get Customer And Orders]  
(@CustomerID nchar(5))  
    -- Add the parameters for the stored procedure here  
AS  
BEGIN  
    -- SET NOCOUNT ON added to prevent extra result sets from  
    -- interfering with SELECT statements.  
    SET NOCOUNT ON;  
    SELECT * FROM Customers AS c WHERE c.CustomerID = @CustomerID    
    SELECT * FROM Orders AS o WHERE o.CustomerID = @CustomerID  
END拖到设计器代码如下:  
 
[Function(Name="dbo.[Get Customer And Orders]")]  
public ISingleResult  
Get_Customer_And_Orders([Parameter(Name="CustomerID",  
DbType="NChar(5)")] string customerID)  
{  
     IExecuteResult result = this.ExecuteMethodCall(this,  
     ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID);  
     return ((ISingleResult)  
     (result.ReturnValue));  
}同样,我们要修改自动生成的代码:  
 
[Function(Name="dbo.[Get Customer And Orders]")]  
[ResultType(typeof(CustomerResultSet))]  
[ResultType(typeof(OrdersResultSet))]  
public IMultipleResults Get_Customer_And_Orders  
([Parameter(Name="CustomerID",DbType="NChar(5)")]  
string customerID)  
{  
    IExecuteResult result = this.ExecuteMethodCall(this,  
    ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID);  
    return ((IMultipleResults)(result.ReturnValue));  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.

同样,自己手写类,让其存储过程返回各自的结果集。

CustomerResultSet类代码在这里展开

public partial class CustomerResultSet  
{  
 
    private string _CustomerID;  
    private string _CompanyName;  
    private string _ContactName;  
    private string _ContactTitle;  
    private string _Address;  
    private string _City;  
    private string _Region;  
    private string _PostalCode;  
    private string _Country;  
    private string _Phone;  
    private string _Fax;  
    public CustomerResultSet()  
    {  
    }  
    [Column(Storage = "_CustomerID", DbType = "NChar(5)")]  
    public string CustomerID  
    {  
        get { return this._CustomerID; }  
        set 
        {  
            if ((this._CustomerID != value))  
                this._CustomerID = value;  
        }  
    }  
    [Column(Storage = "_CompanyName", DbType = "NVarChar(40)")]  
    public string CompanyName  
    {  
        get { return this._CompanyName; }  
        set 
        {  
            if ((this._CompanyName != value))  
                this._CompanyName = value;  
        }  
    }  
    [Column(Storage = "_ContactName", DbType = "NVarChar(30)")]  
    public string ContactName  
    {  
        get { return this._ContactName; }  
        set 
        {  
            if ((this._ContactName != value))  
                this._ContactName = value;  
        }  
    }  
    [Column(Storage = "_ContactTitle", DbType = "NVarChar(30)")]  
    public string ContactTitle  
    {  
        get { return this._ContactTitle; }  
        set 
        {  
            if ((this._ContactTitle != value))  
                this._ContactTitle = value;  
        }  
    }  
    [Column(Storage = "_Address", DbType = "NVarChar(60)")]  
    public string Address  
    {  
        get { return this._Address; }  
        set 
        {  
            if ((this._Address != value))  
                this._Address = value;  
        }  
    }  
    [Column(Storage = "_City", DbType = "NVarChar(15)")]  
    public string City  
    {  
        get { return this._City; }  
        set 
        {  
            if ((this._City != value))  
                this._City = value;  
        }  
    }  
    [Column(Storage = "_Region", DbType = "NVarChar(15)")]  
    public string Region  
    {  
        get { return this._Region; }  
        set 
        {  
            if ((this._Region != value))  
                this._Region = value;  
        }  
    }  
    [Column(Storage = "_PostalCode", DbType = "NVarChar(10)")]  
    public string PostalCode  
    {  
        get { return this._PostalCode; }  
        set 
        {  
            if ((this._PostalCode != value))  
                this._PostalCode = value;  
        }  
    }  
    [Column(Storage = "_Country", DbType = "NVarChar(15)")]  
    public string Country  
    {  
        get { return this._Country; }  
        set 
        {  
            if ((this._Country != value))  
                this._Country = value;  
        }  
    }  
    [Column(Storage = "_Phone", DbType = "NVarChar(24)")]  
    public string Phone  
    {  
        get { return this._Phone; }  
        set 
        {  
            if ((this._Phone != value))  
                this._Phone = value;  
        }  
    }  
 
    [Column(Storage = "_Fax", DbType = "NVarChar(24)")]  
    public string Fax  
    {  
        get { return this._Fax; }  
        set 
        {  
            if ((this._Fax != value))  
                this._Fax = value;  
        }  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.

OrdersResultSet类 代码在这里展开

public partial class OrdersResultSet  
{  
    private System.Nullable<int> _OrderID;  
    private string _CustomerID;  
    private System.Nullable<int> _EmployeeID;  
    private System.Nullable _OrderDate;  
    private System.Nullable _RequiredDate;  
    private System.Nullable _ShippedDate;  
    private System.Nullable<int> _ShipVia;  
    private System.Nullable<decimal> _Freight;  
    private string _ShipName;  
    private string _ShipAddress;  
    private string _ShipCity;  
    private string _ShipRegion;  
    private string _ShipPostalCode;  
    private string _ShipCountry;  
    public OrdersResultSet()  
    {  
    }  
    [Column(Storage = "_OrderID", DbType = "Int")]  
    public System.Nullable<int> OrderID  
    {  
        get { return this._OrderID; }  
        set 
        {  
            if ((this._OrderID != value))  
                this._OrderID = value;  
        }  
    }  
    [Column(Storage = "_CustomerID", DbType = "NChar(5)")]  
    public string CustomerID  
    {  
        get { return this._CustomerID; }  
        set 
        {  
            if ((this._CustomerID != value))  
                this._CustomerID = value;  
        }  
    }  
    [Column(Storage = "_EmployeeID", DbType = "Int")]  
    public System.Nullable<int> EmployeeID  
    {  
        get { return this._EmployeeID; }  
        set 
        {  
            if ((this._EmployeeID != value))  
                this._EmployeeID = value;  
        }  
    }  
    [Column(Storage = "_OrderDate", DbType = "DateTime")]  
    public System.Nullable OrderDate  
    {  
        get { return this._OrderDate; }  
        set 
        {  
            if ((this._OrderDate != value))  
                this._OrderDate = value;  
        }  
    }  
    [Column(Storage = "_RequiredDate", DbType = "DateTime")]  
    public System.Nullable RequiredDate  
    {  
        get { return this._RequiredDate; }  
        set 
        {  
            if ((this._RequiredDate != value))  
                this._RequiredDate = value;  
        }  
    }  
    [Column(Storage = "_ShippedDate", DbType = "DateTime")]  
    public System.Nullable ShippedDate  
    {  
        get { return this._ShippedDate; }  
        set 
        {  
            if ((this._ShippedDate != value))  
                this._ShippedDate = value;  
        }  
    }  
    [Column(Storage = "_ShipVia", DbType = "Int")]  
    public System.Nullable<int> ShipVia  
    {  
        get { return this._ShipVia; }  
        set 
        {  
            if ((this._ShipVia != value))  
                this._ShipVia = value;  
        }  
    }  
    [Column(Storage = "_Freight", DbType = "Money")]  
    public System.Nullable<decimal> Freight  
    {  
        get { return this._Freight; }  
        set 
        {  
            if ((this._Freight != value))  
                this._Freight = value;  
        }  
    }  
    [Column(Storage = "_ShipName", DbType = "NVarChar(40)")]  
    public string ShipName  
    {  
        get { return this._ShipName; }  
        set 
        {  
            if ((this._ShipName != value))  
                this._ShipName = value;  
        }  
    }  
    [Column(Storage = "_ShipAddress", DbType = "NVarChar(60)")]  
    public string ShipAddress  
    {  
        get { return this._ShipAddress; }  
        set 
        {  
            if ((this._ShipAddress != value))  
                this._ShipAddress = value;  
        }  
    }  
    [Column(Storage = "_ShipCity", DbType = "NVarChar(15)")]  
    public string ShipCity  
    {  
        get { return this._ShipCity; }  
        set 
        {  
            if ((this._ShipCity != value))  
                this._ShipCity = value;  
        }  
    }  
    [Column(Storage = "_ShipRegion", DbType = "NVarChar(15)")]  
    public string ShipRegion  
    {  
        get { return this._ShipRegion; }  
        set 
        {  
            if ((this._ShipRegion != value))  
                this._ShipRegion = value;  
        }  
    }  
    [Column(Storage = "_ShipPostalCode", DbType = "NVarChar(10)")]  
    public string ShipPostalCode  
    {  
        get { return this._ShipPostalCode; }  
        set 
        {  
            if ((this._ShipPostalCode != value))  
                this._ShipPostalCode = value;  
        }  
    }  
 
    [Column(Storage = "_ShipCountry", DbType = "NVarChar(15)")]  
    public string ShipCountry  
    {  
        get { return this._ShipCountry; }  
        set 
        {  
            if ((this._ShipCountry != value))  
                this._ShipCountry = value;  
        }  
    }  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.

这时,只要调用就可以了。

IMultipleResults result = db.Get_Customer_And_Orders("SEVES");  
//返回Customer结果集  
IEnumerable customer =   
result.GetResult();  
//返回Orders结果集  
IEnumerable orders =   
 result.GetResult();  
//在这里,我们读取CustomerResultSet中的数据  
foreach (CustomerResultSet cust in customer)  
{  
    Console.WriteLine(cust.CustomerID);  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

语句描述:这个实例使用存储过程返回客户“SEVES”及其所有订单。

5.LINQ to SQL存储过程之带输出参数

LINQ to SQL 将输出参数映射到引用参数,并且对于值类型,它将参数声明为可以为 null。

下面的示例带有单个输入参数(客户 ID)并返回一个输出参数(该客户的总销售额)。

ALTER PROCEDURE [dbo].[CustOrderTotal]   
@CustomerID nchar(5),  
@TotalSales money OUTPUT  
AS  
SELECT @TotalSales = SUM(OD.UNITPRICE*(1-OD.DISCOUNT) * OD.QUANTITY)  
FROM ORDERS O, "ORDER DETAILS" OD  
where O.CUSTOMERID = @CustomerID AND O.ORDERID = OD.ORDERID 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

把这个存储过程拖到设计器中,

其生成代码如下:

[Function(Name="dbo.CustOrderTotal")]  
public int CustOrderTotal(  
[Parameter(Name="CustomerID", DbType="NChar(5)")]string customerID,  
[Parameter(Name="TotalSales", DbType="Money")]  
  ref System.Nullable<decimal> totalSales)  
{  
    IExecuteResult result = this.ExecuteMethodCall(this,  
    ((MethodInfo)(MethodInfo.GetCurrentMethod())),  
    customerID, totalSales);  
    totalSales = ((System.Nullable<decimal>)  
    (result.GetParameterValue(1)));  
    return ((int)(result.ReturnValue));  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

我们使用下面的语句调用此存储过程:注意:输出参数是按引用传递的,以支持参数为“in/out”的方案。在这种情况下,参数仅为“out”。 

decimal? totalSales = 0;  
string customerID = "ALFKI";  
db.CustOrderTotal(customerID, ref totalSales);  
Console.WriteLine("Total Sales for Customer '{0}' = {1:C}",   
customerID, totalSales); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

语句描述:这个实例使用返回 Out 参数的存储过程。

好了,LINQ to SQL存储过程就说到这里了,其增删改操作同理。相信大家通过这5个实例理解了LINQ to SQL存储过程。

【编辑推荐】

  1. 详谈Linq查询结果分析的方法
  2. 简简单单学习Linq查询语法
  3. 详细阐述Linq插入数据的操作方法
  4. 浅析Linq插入数据的实现方法
  5. 简单解决Linq多条件组合问题
责任编辑:阡陌 来源: 博客园
相关推荐

2009-09-17 15:51:39

Linq to sql

2009-09-09 14:40:43

Linq to sql

2009-09-15 14:52:15

linq级联删除

2009-09-07 16:25:14

Linq To SQL

2009-09-08 16:20:12

LINQ to SQL

2009-09-16 09:56:42

LINQ to SQL

2009-09-17 10:04:32

LINQ存储过程

2009-09-15 11:08:01

LinQ调用存储过程

2009-09-09 10:54:52

Linq存储过程返回

2009-09-15 10:59:10

LinQ to SQL

2009-09-13 19:24:33

LINQ存储过程

2009-09-17 10:40:23

linq存储过程

2009-09-17 11:32:52

LINQ调用存储过程

2011-10-10 16:44:37

分页数据库

2009-09-14 15:12:40

LINQ to XML

2009-09-09 16:21:31

Linq使用sqlme

2009-09-14 10:13:02

LINQ查询操作

2009-09-17 13:15:20

LINQ查询

2009-09-16 16:59:05

LINQ to XML

2009-09-10 14:37:57

LINQ匿名类型
点赞
收藏

51CTO技术栈公众号