LINQ to SQL语句(20)之存储过程

30 篇文章 0 订阅

http://www.prg-cn.com/article-4438-1.html

存储过程

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

1.标量返回

在数据库中,有名为 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
  10. 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));
  8. }
复制代码

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

  1. int count = db.CustomersCountByRegion ("WA");
  2. Console.WriteLine(count);
复制代码

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

2.单一结 果集

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

下面的示例表示一个存储过程,该存储过程返回客户行并使用输入参数 来仅返回将“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
  11. END
复制代码

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

  1. [Function(Name="dbo.[Customers By City]")] 
  2. public ISingleResult<Customers_By_CityResult> 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<Customers_By_CityResult>)
  8.    (result.ReturnValue));
  9. }
复制代码

我们用下面的代码调用:

  1. ISingleResult<Customers_By_CityResult> 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 可以生成所有可能的投影类型,但 它无法获知将以何种顺序返回它们。 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'
  14. END
复制代码

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

  1. [Function (Name="dbo.[Whole Or Partial Customers Set]")]
  2. public ISingleResult<Whole_Or_Partial_Customers_SetResult>
  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<Whole_Or_Partial_Customers_SetResult>)
  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<int> 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.   }
复制代码

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<WholeCustomersSetResult> shape1 =
  4. result.GetResult<WholeCustomersSetResult>();
  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<PartialCustomersSetResult> shape2 =
  12. result.GetResult<PartialCustomersSetResult>();
  13. foreach (PartialCustomersSetResult con in shape2)
  14. {
  15.    Console.WriteLine(con.ContactName);
  16. }
复制代码

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

4.多个结果集

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

下面是一个按顺序返回多个结 果集的存储过程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
复制代码

拖到设计器代码如下:

  1. [Function (Name="dbo.[Get Customer And Orders]")]
  2. public ISingleResult<Get_Customer_And_OrdersResult>
  3. Get_Customer_And_Orders([Parameter(Name="CustomerID",
  4. DbType="NChar(5)")] string customerID)
  5. {
  6.    IExecuteResult result = this.ExecuteMethodCall(this,
  7.    ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID);
  8.    return ((ISingleResult<Get_Customer_And_OrdersResult>)
  9.    (result.ReturnValue));
  10. }
复制代码

同样,我们要修改自动生成的代码 :

  1. [Function(Name="dbo.[Get Customer And Orders] ")]
  2. [ResultType(typeof(CustomerResultSet))]
  3. [ResultType(typeof(OrdersResultSet))]
  4. public IMultipleResults Get_Customer_And_Orders
  5. ([Parameter (Name="CustomerID",DbType="NChar(5)")]
  6. string customerID)
  7. {
  8.   IExecuteResult result = this.ExecuteMethodCall(this,
  9.   ((MethodInfo) (MethodInfo.GetCurrentMethod())), customerID);
  10.   return ((IMultipleResults)(result.ReturnValue));
  11. }
复制代码

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

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<int> _OrderID;
  4.   private string _CustomerID;
  5.   private System.Nullable<int> _EmployeeID;
  6.   private System.Nullable<System.DateTime> _OrderDate;
  7.   private System.Nullable<System.DateTime> _RequiredDate;
  8.   private System.Nullable<System.DateTime> _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<System.DateTime> 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<System.DateTime> 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<System.DateTime> 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.    [Column(Storage = "_ShipCountry", DbType = "NVarChar (15)")]
  151.   public string ShipCountry
  152.   {
  153.      get { return this._ShipCountry; }
  154.     set
  155.     {
  156.       if ((this._ShipCountry != value))
  157.          this._ShipCountry = value;
  158.     }
  159.   }
复制代码

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

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

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

5.带输出参数

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));
  13. }
复制代码

我们使用下面的语句调用此存储过 程:注意:输出参数是按引用传递的,以支持参数为“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 参数的存储过程。

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值