C#Dapper:从存储过程中获取多个结果集?

时间:2018-08-27 09:03:24

标签: c# dapper

SP response image

我的SP返回如下数据。当我使用精巧的QueryMultipleAsync时,似乎仅选择第二个结果集,而当使用queryAsync时,它仅选择第一个结果集。请提出建议。预先感谢。

col1    col2    col3
123      name   23.34

time    value   
25:17.0 123 
25:17.0 124 
25:17.0 543 
25:17.0 566 

col1    col2    col3
123     name1   23.34

time    value   
25:17.0 123 
25:17.0 124 
25:17.0 543 
25:17.0 566 

1 个答案:

答案 0 :(得分:1)

使用QueryMulitpleAsync时,您可以一一读取结果集。这是一个对我有用的示例:

[Test]
public async Task MultipleSpResultsWithDapper()
{
    // Act
    using (var conn = new SqlConnection("Data Source=YourDatabase"))
    {
        await conn.OpenAsync();
        var result = await conn.QueryMultipleAsync(
            "YourStoredProcedureName",
            new { param1 = 1, param2 = 2 }, 
            null, null, CommandType.StoredProcedure);

        // read as IEnumerable<dynamic>
        var table1 = await result.ReadAsync();
        var table2 = await result.ReadAsync();

        // read as typed IEnumerable
        var table3 = await result.ReadAsync<Table1>();
        var table4 = await result.ReadAsync<Table2>();

        //Assert
        Assert.IsNotEmpty(table1);
        Assert.IsNotEmpty(table2);
        Assert.IsNotEmpty(table3);
        Assert.IsNotEmpty(table4);
    }
}

实体类:

public class Table1
{
    public int col1 { get; set; }

    public string col2 { get; set; }

    public double col3 { get; set; }
}

public class Table2
{
    public string time { get; set; }

    public int value { get; set; }
}

存储过程声明:

CREATE PROCEDURE [dbo].YourStoredProcedureName
(
    @param1 int, 
    @param2 int
)

希望有帮助。