深度剖析LINQtoSQL存储过程

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

创新互联专业为企业提供江华网站建设、江华做网站、江华网站设计、江华网站制作等企业网站建设、网页设计与制作、江华企业网站模板建站服务,10年江华做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

在我们编写程序中,往往需要一些存储过程,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));
  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”地区中的一组客户。返回的结果集形状取决于传入的参数。如果参数等于 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. [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. &

    名称栏目:深度剖析LINQtoSQL存储过程
    本文来源:http://www.hantingmc.com/qtweb/news15/263315.html

    网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

    广告

    声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联