Welcome

首页 / 软件开发 / LINQ / Linq to SQL之使用存储过程(2)

Linq to SQL之使用存储过程(2)2011-08-04 博客园 紫色阴影本文接着上篇文章Linq to SQL之使用存储过程 (1),继续探讨如何在Linq to SQL中使用存储过程。

在写存储过程的时候,有时候会用到返回值而不是output参数,现在看看怎样取到该返回值呢?

比如这样一个存储过程:

create procedure dbo.linqDemo4
@input varchar(20)
as
select * from customers
return 20

设计器自动生成的函数如下,可以看到并没有提供方式取到该存储过程的返回值:

[Function(Name="dbo.linqDemo4")]
public ISingleResult<Customer> linqDemo4([Parameter(DbType="VarChar(20)")] string input) {
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo) (MethodInfo.GetCurrentMethod())), input);
return ((ISingleResult<Customer>)(result.ReturnValue));
}

只能靠自己了,增加一个函数对它进行包装,以此来取到返回值

public ISingleResult<Customer> linqDemo4WithReturnValue(string input, out int returnValue)
{
ISingleResult<Customer> result = linqDemo4(input);
returnValue = (int)result.ReturnValue;
return (ISingleResult<Customer>)(result);
}

增加一个out参数returnValue,这样就能从结果中取到这个返回值。不知道以后的设计器能不能自动 识别。

有时候会到这种需求,存储过程返回多个结果集合,那么这时候怎么操作才能把结果集分开呢。比如 下面这个存储过程,能不能把Customer和Order的结果集分别取到呢?

create procedure dbo.linqDemo5
as
select * from customers
select * from orders