自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

深度剖析LINQ to SQL存儲過程

開發(fā) 后端
在我們編寫程序中,往往需要一些存儲過程,LINQ to SQL存儲過程中怎么使用呢?也許比原來的更簡單些。本文以NORTHWND.MDF數(shù)據(jù)庫中自帶的幾個存儲過程來理解一下。

本文將從5個方面來學(xué)習LINQ to SQL存儲過程,它們其中有LINQ to SQL存儲過程之標量返回、LINQ to SQL存儲過程之單一結(jié)果集等等。

在我們編寫程序中,往往需要一些存儲過程,LINQ to SQL存儲過程中怎么使用呢?也許比原來的更簡單些。下面我們以NORTHWND.MDF數(shù)據(jù)庫中自帶的幾個存儲過程來理解一下。

1.LINQ to SQL存儲過程之標量返回
在數(shù)據(jù)庫中,有名為Customers Count By Region的存儲過程。該存儲過程返回顧客所在"WA"區(qū)域的數(shù)量。

  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設(shè)計器內(nèi),它自動生成了以下代碼段:

  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));  

我們需要時,直接調(diào)用就可以了,例如:

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

語句描述:這個實例使用存儲過程返回在“WA”地區(qū)的客戶數(shù)。

2.LINQ to SQL存儲過程之單一結(jié)果集
從數(shù)據(jù)庫中返回行集合,并包含用于篩選結(jié)果的輸入?yún)?shù)。 當我們執(zhí)行返回行集合的存儲過程時,會用到結(jié)果類,它存儲從存儲過程中返回的結(jié)果。

下面的示例表示一個存儲過程,該存儲過程返回客戶行并使用輸入?yún)?shù)來僅返回將“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設(shè)計器內(nèi),它自動生成了以下代碼段:

  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));  

我們用下面的代碼調(diào)用:

  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存儲過程之多個可能形狀的單一結(jié)果集

當存儲過程可以返回多個結(jié)果形狀時,返回類型無法強類型化為單個投影形狀。盡管 LINQ to SQL 可以生成所有可能的投影類型,但它無法獲知將以何種順序返回它們。 ResultTypeAttribute 屬性適用于返回多個結(jié)果類型的存儲過程,用以指定該過程可以返回的類型的集合。

在下面的 SQL 代碼示例中,結(jié)果形狀取決于輸入(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設(shè)計器內(nèi),它自動生成了以下代碼段:

  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會把多結(jié)果集存儲過程識別為單結(jié)果集的存儲過程,默認生成的代碼我們要手動修改一下,要求返回多個結(jié)果集,像這樣:

  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.     }  

這樣就可以使用了,下面代碼直接調(diào)用,分別返回各自的結(jié)果集合。

  1. //返回全部Customer結(jié)果集  
  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結(jié)果集  
  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”地區(qū)中的一組客戶。返回的結(jié)果集形狀取決于傳入的參數(shù)。如果參數(shù)等于 1,則返回所有客戶屬性。如果參數(shù)等于 2,則返回ContactName屬性。

4.LINQ to SQL存儲過程之多個結(jié)果集

這種存儲過程可以生成多個結(jié)果形狀,但我們已經(jīng)知道結(jié)果的返回順序。

下面是一個按順序返回多個結(jié)果集的存儲過程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拖到設(shè)計器代碼如下:  
  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. }  

同樣,自己手寫類,讓其存儲過程返回各自的結(jié)果集。

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. }  

這時,只要調(diào)用就可以了。

  1. IMultipleResults result = db.Get_Customer_And_Orders("SEVES");  
  2. //返回Customer結(jié)果集  
  3. IEnumerable customer =   
  4. result.GetResult();  
  5. //返回Orders結(jié)果集  
  6. IEnumerable orders =   
  7.  result.GetResult();  
  8. //在這里,我們讀取CustomerResultSet中的數(shù)據(jù)  
  9. foreach (CustomerResultSet cust in customer)  
  10. {  
  11.     Console.WriteLine(cust.CustomerID);  

語句描述:這個實例使用存儲過程返回客戶“SEVES”及其所有訂單。

5.LINQ to SQL存儲過程之帶輸出參數(shù)

LINQ to SQL 將輸出參數(shù)映射到引用參數(shù),并且對于值類型,它將參數(shù)聲明為可以為 null。

下面的示例帶有單個輸入?yún)?shù)(客戶 ID)并返回一個輸出參數(shù)(該客戶的總銷售額)。

  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 

把這個存儲過程拖到設(shè)計器中,

其生成代碼如下:

  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));  

我們使用下面的語句調(diào)用此存儲過程:注意:輸出參數(shù)是按引用傳遞的,以支持參數(shù)為“in/out”的方案。在這種情況下,參數(shù)僅為“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 參數(shù)的存儲過程。

好了,LINQ to SQL存儲過程就說到這里了,其增刪改操作同理。相信大家通過這5個實例理解了LINQ to SQL存儲過程。

【編輯推薦】

  1. 詳談Linq查詢結(jié)果分析的方法
  2. 簡簡單單學(xué)習Linq查詢語法
  3. 詳細闡述Linq插入數(shù)據(jù)的操作方法
  4. 淺析Linq插入數(shù)據(jù)的實現(xiàn)方法
  5. 簡單解決Linq多條件組合問題
責任編輯:阡陌 來源: 博客園
點贊
收藏

51CTO技術(shù)棧公眾號