BBu*_*rke 3 stored-procedures output-parameter linq-to-sql
在执行存储过程时,是否可以从LINQ To SQL DataContext返回输出参数?
IEnumerable<Address> result =
ExecuteQuery<Address>(((MethodInfo)(MethodInfo.GetCurrentMethod())),
address, pageIndex, pageSize, totalCount);
Run Code Online (Sandbox Code Playgroud)
其中address,pageIndex与pageSize被输入的参数,并且TotalCount是一个输出参数.
如何捕获输出参数?
这是另一种尝试,但再次无法获取参数值:
[Function(Name = "Telecom.AddressSearch")]
private IEnumerable SearchAddress([Parameter(Name = "address", DbType = "varchar")] string address,
[Parameter(Name = "pageIndex", DbType = "int")] int pageIndex,
[Parameter(Name = "pageSize", DbType = "int")] int pageSize,
[Parameter(Name = "totalCount", DbType = "int")] ref int totalCount)
{
IEnumerable result = ExecuteQuery(((MethodInfo)(MethodInfo.GetCurrentMethod())), address, pageIndex, pageSize, totalCount);
return result;
}Run Code Online (Sandbox Code Playgroud)
Scott Guthrie有一篇博客文章描述了如何使用LINQ to SQL来处理存储过程.虽然他的帖子没有直接回答你的问题,但它提供了可能有用的东西的线索.在.dbml类中定义方法时,"LINQ to SQL将SPROC中的'参数'映射为参数参数(ref关键字)." 您可以尝试使用ref关键字并查看totalCount是否得到更新.
IEnumerable r = ExecuteQuery(((MethodInfo)(MethodInfo.GetCurrentMethod())),
address, pageIndex, pageSize, ref totalCount);
Run Code Online (Sandbox Code Playgroud)
更新:
我做了一些更多的研究,并找到了适合你的方法.这是来自MSDN文章.你需要扩展你的DataContext,但这不应该是一个大问题(看起来你可能已经这样做了).查看文章了解更多信息,但基本部分是这样的:
[Function(Name="dbo.CustOrderTotal")]
[return: Parameter(DbType="Int")]
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));
}
Run Code Online (Sandbox Code Playgroud)
'this'在哪里是你的DataContext.
更新2:
IExecuteResult具有ReturnValue属性.它是Object类型,因此您必须将其强制转换才能获得结果,但它应该允许您获取结果的Enumerable.在你的情况下,这样的事情应该工作:
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), address, pageIndex, pageSize, totalCount);
totalCount = ((System.Nullable<decimal>)(result.GetParameterValue(3)));
return (IEnumerable<Address>)result.ReturnValue;
Run Code Online (Sandbox Code Playgroud)