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

M.M*_*ati 1 c# dapper

SP响应图像

我的 SP 返回如下数据。当我使用 dapper 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 
Run Code Online (Sandbox Code Playgroud)

Koz*_*try 6

使用时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);
    }
}
Run Code Online (Sandbox Code Playgroud)

实体类:

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; }
}
Run Code Online (Sandbox Code Playgroud)

存储过程声明:

CREATE PROCEDURE [dbo].YourStoredProcedureName
(
    @param1 int, 
    @param2 int
)
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。