Linq to sql 接收存储过程返回的多个结果集

故事前提。。。。。。。。。。

一、返回顺序结果集

存储过程实例

CREATE PROCEDURE MultipleResultTypesSequentially
AS
select * from products
select * from customers

修改vs生成的存储过程代码

Linq to sql 接收存储过程返回的多个结果集
Linq to sql 接收存储过程返回的多个结果集
[Function(Name="dbo.MultipleResultTypesSequentially")]
    [ResultType(typeof(MultipleResultTypesSequentiallyResult1))]
    [ResultType(typeof(MultipleResultTypesSequentiallyResult2))]
    public IMultipleResults MultipleResultTypesSequentially()
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
        return ((IMultipleResults)(result.ReturnValue));
    }
Linq to sql 接收存储过程返回的多个结果集

 

Linq to sql 接收存储过程返回的多个结果集

调用存储过程

Linq to sql 接收存储过程返回的多个结果集
Linq to sql 接收存储过程返回的多个结果集
IMultipleResults sprocResults =
    db.MultipleResultTypesSequentially();

// First read products.
foreach (Product prod in sprocResults.GetResult<Product>())
{
    Console.WriteLine(prod.ProductID);
}

// Next read customers.
foreach (Customer cust in sprocResults.GetResult<Customer>())
{
    Console.WriteLine(cust.CustomerID);
}
Linq to sql 接收存储过程返回的多个结果集

 

Linq to sql 接收存储过程返回的多个结果集

二、多个结果返回集(如不通参数返回不通类型结果集)

存储过程实例

Linq to sql 接收存储过程返回的多个结果集
CREATE PROCEDURE VariableResultShapes(@shape int)
AS
if(@shape = 1)
    select CustomerID, ContactTitle, CompanyName from customers
else if(@shape = 2)
    select OrderID, ShipName from orders
Linq to sql 接收存储过程返回的多个结果集

C# 存储过程代码 

Linq to sql 接收存储过程返回的多个结果集
[Function(Name="dbo.VariableResultShapes")]
    [ResultType(typeof(VariableResultShapesResult1))]
    [ResultType(typeof(VariableResultShapesResult2))]
    public IMultipleResults VariableResultShapes([Parameter(DbType="Int")] System.Nullable<int> shape)
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), shape);
        return ((IMultipleResults)(result.ReturnValue));
    }
Linq to sql 接收存储过程返回的多个结果集

存储过程调用

Linq to sql 接收存储过程返回的多个结果集
IMultipleResults result = db.VariableResultShapes(1);

// Iterate through the list and write results (the company names)
// to the console.
foreach(VariableResultShapesResult1 compName in
    result.GetResult<VariableResultShapesResult1>())
{
    Console.WriteLine(compName.CompanyName);
}

// Pause to view company names; press Enter to continue.
Console.ReadLine();

// Assign the results of the procedure with an argument
// of (2) to local variable ‘result‘.
IMultipleResults result2 = db.VariableResultShapes(2);

// Iterate through the list and write results (the order IDs)
// to the console.
foreach (VariableResultShapesResult2 ord in
    result2.GetResult<VariableResultShapesResult2>())
{
    Console.WriteLine(ord.OrderID);
}
Linq to sql 接收存储过程返回的多个结果集

 最后说一句:其实就是很不要脸的把msdn上的东西拿过来了

参考:http://msdn.microsoft.com/zh-cn/library/system.data.linq.imultipleresults(v=vs.110).aspx

Linq to sql 接收存储过程返回的多个结果集

上一篇:【原创】PHP访问MySQL查询超时处理


下一篇:SQL笔记 - 解决CTE定位点类型和递归部分的类型不匹配