深度剖析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"区域的数量。

  1. ALTER PROCEDURE [dbo].[NonRowset]  
  2.     (@param1 NVARCHAR(15))  
  3. AS  
  4. BEGIN  
  5.     SET NOCOUNT ON;  
  6.      DECLARE @count int 
  7.      SELECT @count = COUNT(*)FROM Customers   
  8.      WHERECustomers.Region = @Param1  
  9.      RETURN @count  

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

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

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

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

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

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

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

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

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

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

我们用下面的代码调用:

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

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

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

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

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

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

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

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

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

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

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

 代码在这里展开

  1. public partial class WholeCustomersSetResult  
  2. {  
  3.     private string _CustomerID;  
  4.     private string _CompanyName;  
  5.     private string _ContactName;  
  6.     private string _ContactTitle;  
  7.     private string _Address;  
  8.     private string _City;  
  9.     private string _Region;  
  10.     private string _PostalCode;  
  11.     private string _Country;  
  12.     private string _Phone;  
  13.     private string _Fax;  
  14.     public WholeCustomersSetResult()  
  15.     {  
  16.     }  
  17.     [Column(Storage = "_CustomerID", DbType = "NChar(5)")]  
  18.     public string CustomerID  
  19.     {  
  20.         get { return this._CustomerID; }  
  21.         set 
  22.         {  
  23.             if ((this._CustomerID != value))  
  24.                 this._CustomerID = value;  
  25.         }  
  26.     }  
  27.     [Column(Storage = "_CompanyName", DbType = "NVarChar(40)")]  
  28.     public string CompanyName  
  29.     {  
  30.         get { return this._CompanyName; }  
  31.         set 
  32.         {  
  33.             if ((this._CompanyName != value))  
  34.                 this._CompanyName = value;  
  35.         }  
  36.     }  
  37.     [Column(Storage = "_ContactName", DbType = "NVarChar(30)")]  
  38.     public string ContactName  
  39.     {  
  40.         get { return this._ContactName; }  
  41.         set 
  42.         {  
  43.             if ((this._ContactName != value))  
  44.                 this._ContactName = value;  
  45.         }  
  46.     }  
  47.     [Column(Storage = "_ContactTitle", DbType = "NVarChar(30)")]  
  48.     public string ContactTitle  
  49.     {  
  50.         get { return this._ContactTitle; }  
  51.         set 
  52.         {  
  53.             if ((this._ContactTitle != value))  
  54.                 this._ContactTitle = value;  
  55.         }  
  56.     }  
  57.     [Column(Storage = "_Address", DbType = "NVarChar(60)")]  
  58.     public string Address  
  59.     {  
  60.         get { return this._Address; }  
  61.         set 
  62.         {  
  63.             if ((this._Address != value))  
  64.                 this._Address = value;  
  65.         }  
  66.     }  
  67.     [Column(Storage = "_City", DbType = "NVarChar(15)")]  
  68.     public string City  
  69.     {  
  70.         get { return this._City; }  
  71.         set 
  72.         {  
  73.             if ((this._City != value))  
  74.                 this._City = value;  
  75.         }  
  76.     }  
  77.     [Column(Storage = "_Region", DbType = "NVarChar(15)")]  
  78.     public string Region  
  79.     {  
  80.         get { return this._Region; }  
  81.         set 
  82.         {  
  83.             if ((this._Region != value))  
  84.                 this._Region = value;  
  85.         }  
  86.     }  
  87.     [Column(Storage = "_PostalCode", DbType = "NVarChar(10)")]  
  88.     public string PostalCode  
  89.     {  
  90.         get { return this._PostalCode; }  
  91.         set 
  92.         {  
  93.             if ((this._PostalCode != value))  
  94.                 this._PostalCode = value;  
  95.         }  
  96.     }  
  97.     [Column(Storage = "_Country", DbType = "NVarChar(15)")]  
  98.     public string Country  
  99.     {  
  100.         get { return this._Country; }  
  101.         set 
  102.         {  
  103.             if ((this._Country != value))  
  104.                 this._Country = value;  
  105.         }  
  106.     }  
  107.     [Column(Storage = "_Phone", DbType = "NVarChar(24)")]  
  108.     public string Phone  
  109.     {  
  110.         get { return this._Phone; }  
  111.         set 
  112.         {  
  113.             if ((this._Phone != value))  
  114.                 this._Phone = value;  
  115.         }  
  116.     }  
  117.     [Column(Storage = "_Fax", DbType = "NVarChar(24)")]  
  118.     public string Fax  
  119.     {  
  120.         get { return this._Fax; }  
  121.         set 
  122.         {  
  123.             if ((this._Fax != value))  
  124.                 this._Fax = value;  
  125.         }  
  126.     }  

PartialCustomersSetResult类 如下:(点击展开)

代码在这里展开

  1. public partial class PartialCustomersSetResult  
  2. {  
  3.     private string _CustomerID;  
  4.     private string _ContactName;  
  5.     private string _CompanyName;  
  6.     public PartialCustomersSetResult()  
  7.     {  
  8.     }  
  9.     [Column(Storage = "_CustomerID", DbType = "NChar(5)")]  
  10.     public string CustomerID  
  11.     {  
  12.         get { return this._CustomerID; }  
  13.         set 
  14.         {  
  15.             if ((this._CustomerID != value))  
  16.                 this._CustomerID = value;  
  17.         }  
  18.     }  
  19.     [Column(Storage = "_ContactName", DbType = "NVarChar(30)")]  
  20.     public string ContactName  
  21.     {  
  22.         get { return this._ContactName; }  
  23.         set 
  24.         {  
  25.             if ((this._ContactName != value))  
  26.                 this._ContactName = value;  
  27.         }  
  28.     }  
  29.     [Column(Storage = "_CompanyName", DbType = "NVarChar(40)")]  
  30.     public string CompanyName  
  31.     {  
  32.         get { return this._CompanyName; }  
  33.         set 
  34.         {  
  35.             if ((this._CompanyName != value))  
  36.                 this._CompanyName = value;  
  37.         }  
  38.     }  

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

  1. //返回全部Customer结果集  
  2. IMultipleResults result = db.Whole_Or_Partial_Customers_Set(1);  
  3. IEnumerable shape1 =  
  4.  result.GetResult();  
  5. foreach (WholeCustomersSetResult compName in shape1)  
  6. {  
  7.     Console.WriteLine(compName.CompanyName);  
  8. }  
  9. //返回部分Customer结果集  
  10. result = db.Whole_Or_Partial_Customers_Set(2);  
  11. IEnumerable shape2 =  
  12.  result.GetResult();  
  13. foreach (PartialCustomersSetResult con in shape2)  
  14. {  
  15.     Console.WriteLine(con.ContactName);  

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

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

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

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

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

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

CustomerResultSet类代码在这里展开

  1. public partial class CustomerResultSet  
  2. {  
  3.  
  4.     private string _CustomerID;  
  5.     private string _CompanyName;  
  6.     private string _ContactName;  
  7.     private string _ContactTitle;  
  8.     private string _Address;  
  9.     private string _City;  
  10.     private string _Region;  
  11.     private string _PostalCode;  
  12.     private string _Country;  
  13.     private string _Phone;  
  14.     private string _Fax;  
  15.     public CustomerResultSet()  
  16.     {  
  17.     }  
  18.     [Column(Storage = "_CustomerID", DbType = "NChar(5)")]  
  19.     public string CustomerID  
  20.     {  
  21.         get { return this._CustomerID; }  
  22.         set 
  23.         {  
  24.             if ((this._CustomerID != value))  
  25.                 this._CustomerID = value;  
  26.         }  
  27.     }  
  28.     [Column(Storage = "_CompanyName", DbType = "NVarChar(40)")]  
  29.     public string CompanyName  
  30.     {  
  31.         get { return this._CompanyName; }  
  32.         set 
  33.         {  
  34.             if ((this._CompanyName != value))  
  35.                 this._CompanyName = value;  
  36.         }  
  37.     }  
  38.     [Column(Storage = "_ContactName", DbType = "NVarChar(30)")]  
  39.     public string ContactName  
  40.     {  
  41.         get { return this._ContactName; }  
  42.         set 
  43.         {  
  44.             if ((this._ContactName != value))  
  45.                 this._ContactName = value;  
  46.         }  
  47.     }  
  48.     [Column(Storage = "_ContactTitle", DbType = "NVarChar(30)")]  
  49.     public string ContactTitle  
  50.     {  
  51.         get { return this._ContactTitle; }  
  52.         set 
  53.         {  
  54.             if ((this._ContactTitle != value))  
  55.                 this._ContactTitle = value;  
  56.         }  
  57.     }  
  58.     [Column(Storage = "_Address", DbType = "NVarChar(60)")]  
  59.     public string Address  
  60.     {  
  61.         get { return this._Address; }  
  62.         set 
  63.         {  
  64.             if ((this._Address != value))  
  65.                 this._Address = value;  
  66.         }  
  67.     }  
  68.     [Column(Storage = "_City", DbType = "NVarChar(15)")]  
  69.     public string City  
  70.     {  
  71.         get { return this._City; }  
  72.         set 
  73.         {  
  74.             if ((this._City != value))  
  75.                 this._City = value;  
  76.         }  
  77.     }  
  78.     [Column(Storage = "_Region", DbType = "NVarChar(15)")]  
  79.     public string Region  
  80.     {  
  81.         get { return this._Region; }  
  82.         set 
  83.         {  
  84.             if ((this._Region != value))  
  85.                 this._Region = value;  
  86.         }  
  87.     }  
  88.     [Column(Storage = "_PostalCode", DbType = "NVarChar(10)")]  
  89.     public string PostalCode  
  90.     {  
  91.         get { return this._PostalCode; }  
  92.         set 
  93.         {  
  94.             if ((this._PostalCode != value))  
  95.                 this._PostalCode = value;  
  96.         }  
  97.     }  
  98.     [Column(Storage = "_Country", DbType = "NVarChar(15)")]  
  99.     public string Country  
  100.     {  
  101.         get { return this._Country; }  
  102.         set 
  103.         {  
  104.             if ((this._Country != value))  
  105.                 this._Country = value;  
  106.         }  
  107.     }  
  108.     [Column(Storage = "_Phone", DbType = "NVarChar(24)")]  
  109.     public string Phone  
  110.     {  
  111.         get { return this._Phone; }  
  112.         set 
  113.         {  
  114.             if ((this._Phone != value))  
  115.                 this._Phone = value;  
  116.         }  
  117.     }  
  118.  
  119.     [Column(Storage = "_Fax", DbType = "NVarChar(24)")]  
  120.     public string Fax  
  121.     {  
  122.         get { return this._Fax; }  
  123.         set 
  124.         {  
  125.             if ((this._Fax != value))  
  126.                 this._Fax = value;  
  127.         }  
  128.     }  

OrdersResultSet类 代码在这里展开

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

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

  1. IMultipleResults result = db.Get_Customer_And_Orders("SEVES");  
  2. //返回Customer结果集  
  3. IEnumerable customer =   
  4. result.GetResult();  
  5. //返回Orders结果集  
  6. IEnumerable orders =   
  7.  result.GetResult();  
  8. //在这里,我们读取CustomerResultSet中的数据  
  9. foreach (CustomerResultSet cust in customer)  
  10. {  
  11.     Console.WriteLine(cust.CustomerID);  

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

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

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

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

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

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

其生成代码如下:

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

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

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

语句描述:这个实例使用返回 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-16 16:59:05

LINQ to XML

2009-09-10 14:37:57

LINQ匿名类型

2009-09-17 13:15:20

LINQ查询

2009-09-09 16:21:31

Linq使用sqlme

2009-09-14 10:13:02

LINQ查询操作

2009-09-14 15:12:40

LINQ to XML
点赞
收藏

51CTO技术栈公众号