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

问题描述 投票:-1回答:1

我的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 
c# dapper
1个回答
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
)

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.