Linq To Sql进阶系列(五)Store Procedure篇
[1] Linq To Sql进阶系列(五)Store Procedure篇
[2] Linq To Sql进阶系列(五)Store Procedure篇
[3] Linq To Sql进阶系列(五)Store Procedure篇
[4] Linq To Sql进阶系列(五)Store Procedure篇
[2] Linq To Sql进阶系列(五)Store Procedure篇
[3] Linq To Sql进阶系列(五)Store Procedure篇
[4] Linq To Sql进阶系列(五)Store Procedure篇
系列文章导航:
Linq To Sql进阶系列(四)User Define Function篇
Linq To Sql进阶系列(五)Store Procedure篇
Linq To Sql进阶系列(六)用object的动态查询与保存log篇
Linq To Sql进阶系列(七)动态查询续及CLR与SQL在某些细节上的差别
3,OutParameters
似乎没有什么好讲的,很简单,当作ref 的函数参数输出的。其也只是在生成的函数里加了这么一句
outParameter = ((System.Nullable<int>)(result.GetParameterValue(1))); 调用result.GetParameterValue方法,大家要记住这个哦。
4,Return Value
呀,把return value丢那里了。的确,Linq曾舍弃过return value.后来在qa的坚持下,dev们决定保留了它。但是,需要你自己去更改code,才能获得。我们可以从下面这个sprocs上获得灵感。
CREATE PROCEDURE [dbo].[CustOrderTotal]
@CustomerID nchar(5),
@TotalSales money OUTPUT
AS
SELECT @TotalSales = SUM(OD.UNITPRICE*(1-OD.DISCOUNT) * OD.QUANTITY)
FROM ORDERS O, "ORDER DETAILS" OD
where O.CUSTOMERID = @CustomerID AND O.ORDERID = OD.ORDERID
@CustomerID nchar(5),
@TotalSales money OUTPUT
AS
SELECT @TotalSales = SUM(OD.UNITPRICE*(1-OD.DISCOUNT) * OD.QUANTITY)
FROM ORDERS O, "ORDER DETAILS" OD
where O.CUSTOMERID = @CustomerID AND O.ORDERID = OD.ORDERID
其影射的code为
[Function(Name="dbo.CustOrderTotal")]
public int CustOrderTotal([Parameter(Name="CustomerID", DbType="NChar(5)")]
string customerID, [Parameter(Name="TotalSales", DbType="Money")]
ref System.Nullable<decimal> totalSales)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID, totalSales);
totalSales = ((System.Nullable<decimal>)(result.GetParameterValue(1)));
return ((int)(result.ReturnValue));
}
public int CustOrderTotal([Parameter(Name="CustomerID", DbType="NChar(5)")]
string customerID, [Parameter(Name="TotalSales", DbType="Money")]
ref System.Nullable<decimal> totalSales)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID, totalSales);
totalSales = ((System.Nullable<decimal>)(result.GetParameterValue(1)));
return ((int)(result.ReturnValue));
}
因为该sprocs并没有rowset返回,其最后只剩返回值了。其是将result.RenturnValue强制转化为int型,以得到sprocs的整形返回值。那对于SingleResultSet和MultipleResultSets的,该如何获取它的返回值呢?那只有自己动手,修改code了。就是自己强制转换,而后通过out 参数输出。比如用第一个sprocs
[Function(Name = "dbo.[Customers By City]")]
public ISingleResult<Customers_By_CityResult1> Customers_By_City([Parameter(
DbType = "NVarChar(20)")] string param1)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), param1);
return ((ISingleResult<Customers_By_CityResult1>)(result.ReturnValue));
}
public ISingleResult<Customers_By_CityResult1> Customers_By_City2(string para,
out int returnValue)
{
ISingleResult<Customers_By_CityResult1> result = this.Customers_By_City(para);
returnValue = (int)result.ReturnValue;
return ((ISingleResult<Customers_By_CityResult1>)result);
}
public ISingleResult<Customers_By_CityResult1> Customers_By_City([Parameter(
DbType = "NVarChar(20)")] string param1)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), param1);
return ((ISingleResult<Customers_By_CityResult1>)(result.ReturnValue));
}
public ISingleResult<Customers_By_CityResult1> Customers_By_City2(string para,
out int returnValue)
{
ISingleResult<Customers_By_CityResult1> result = this.Customers_By_City(para);
returnValue = (int)result.ReturnValue;
return ((ISingleResult<Customers_By_CityResult1>)result);
}
测试一下
DataClasses1DataContext db = new DataClasses1DataContext();
db.Log = Console.Out;
int returnValue = -1;
var q = db.Customers_By_City2("London",out returnValue);
Console.WriteLine(returnValue);
db.Log = Console.Out;
int returnValue = -1;
var q = db.Customers_By_City2("London",out returnValue);
Console.WriteLine(returnValue);
也可以使用result.GetParameterValue方法获取返回值。只是linq会检查影射函数的参数个数。这个使用起来比较麻烦。可以这么使用,比如,有一个sprocs,有2个参数,其中有一个out的参数。这个out的参数,可以不做任何操作。在影射后,修改其code。这两个参数的位标是从0开始,依次递增。其code本来为outParameter = ((System.Nullable<int>)(result.GetParameterValue(1))); 将其索引修改位为2,就是return value了。再大了又该抛了。