日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
深度剖析LINQtoSQL存儲過程

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

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供江華網(wǎng)站建設、江華做網(wǎng)站、江華網(wǎng)站設計、江華網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、江華企業(yè)網(wǎng)站模板建站服務,10年江華做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。

在我們編寫程序中,往往需要一些存儲過程,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設計器內,它自動生成了以下代碼段:

 
 
 
  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”地區(qū)的客戶數(shù)。

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

下面的示例表示一個存儲過程,該存儲過程返回客戶行并使用輸入?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設計器內,它自動生成了以下代碼段:

 
 
 
  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));
  9. }

我們用下面的代碼調用:

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

語句描述:這個實例使用存儲過程返回在倫敦的客戶的 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 param1)
  5. {
  6.     IExecuteResult result = this.ExecuteMethodCall(this, 
  7.     ((MethodInfo)(MethodInfo.GetCurrentMethod())), param1);
  8.     return ((ISingleResult )  
  9.     (result.ReturnValue));
  10. }

但是,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 param1)
  6. {
  7.     IExecuteResult result = this.ExecuteMethodCall(this, 
  8.     ((MethodInfo)(MethodInfo.GetCurrentMethod())), param1);
  9.     return ((IMultipleResults)(result.ReturnValue));
  10. }

我們分別定義了兩個分部類,用于指定返回的類型。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.     }
  127. }

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

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

 
 
 
  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);
  16. }

語句描述:這個實例使用存儲過程返回“WA”地區(qū)中的一組客戶。返回的結果集形狀取決于傳入的參數(shù)。如果參數(shù)等于 1,則返回所有客戶屬性。如果參數(shù)等于 2,則返回ContactName屬性。

4.LINQ to SQL存儲過程之多個結果集

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

下面是一個按順序返回多個結果集的存儲過程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. [Function(Name="dbo.[Get Customer And Orders]")]
  13. public ISingleResult  
  14. Get_Customer_And_Orders([Parameter(Name="CustomerID",
  15. DbType="NChar(5)")] string customerID)
  16. {
  17.      IExecuteResult result = this.ExecuteMethodCall(this,
  18.      ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID);
  19.      return ((ISingleResult )  
  20.      (result.ReturnValue));
  21. }同樣,我們要修改自動生成的代碼:
  22. [Function(Name="dbo.[Get Customer And Orders]")]
  23. [ResultType(typeof(CustomerResultSet))]
  24. [ResultType(typeof(OrdersResultSet))]
  25. public IMultipleResults Get_Customer_And_Orders
  26. ([Parameter(Name="CustomerID",DbType="NChar(5)")]
  27. string customerID)
  28. {
  29.     IExecuteResult result = this.ExecuteMethodCall(this,
  30.     ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID);
  31.     return ((IMultipleResults)(result.ReturnValue));
  32. }

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

CustomerResultSet類代碼在這里展開

 
 
 
  1. public partial class CustomerResultSet
  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 CustomerResultSet()
  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.     }
  127. }

OrdersResultSet類 代碼在這里展開

 
 
 
  1. public partial class OrdersResultSet
  2. {
  3.     private System.Nullable _OrderID;
  4.     private string _CustomerID;
  5.     private System.Nullable _EmployeeID;
  6.     private System.Nullable  _OrderDate;  
  7.     private System.Nullable  _RequiredDate;  
  8.     private System.Nullable  _ShippedDate;  
  9.     private System.Nullable _ShipVia;
  10.     private System.Nullable _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 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 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. &
    網(wǎng)站欄目:深度剖析LINQtoSQL存儲過程
    文章地址:http://www.5511xx.com/article/dhjshcj.html