获取从dapper .QueryMultiple方法返回的结果集/表的计数

时间:2015-04-09 13:26:53

标签: c# visual-studio dapper

使用Dapper进行多次查询时:

 var result = sqlConnection.QueryMultiple(query, Parameters, commandType: commandType);

如何获取查询返回的表计数?它有两个重载的.Read()方法实现,每次调用时都会移动到下一个可用的结果集(No result.Count()属性)。最终我想把这个数字放在一个循环中,以迭代从查询返回的表的数量。

4 个答案:

答案 0 :(得分:3)

`

var reader = this.DbConnection.QueryMultipleAsync(sql,Params,commandType:CommandType.StoredProcedure).Result;

if(reader.IsConsumed == false)
        {
            DeviceTypeReport = reader?.ReadAsync<dynamic>().Result;
        }

`

这可能是你希望它有所帮助的。

答案 1 :(得分:1)

这可能是您在寻找希望对您有所帮助的东西。

    List<dynamic> data = new List<dynamic>();
    while (reader.IsConsumed == false)
    {
       data.Add(await reader?.ReadAsync<dynamic>());
    }
    int totalRecordSet = data.Count;

答案 2 :(得分:0)

public List NotificationExecuteMultiple(OutdoorRequest objreq, IConfiguration配置)

    {
        var lst = new List<dynamic>();
        using (DbConnection connection = new MySqlConnection(configuration.GetConnectionString("SquareHrConn")))
        {
            using (var dr = connection.QueryMultiple(ProcedureName, GetParamenter(objreq), commandType: CommandType.StoredProcedure))
            {
                while (dr.IsConsumed == false)
                {
                    lst.Add(dr.Read());
                }
            }
        }
        return lst;
    }

答案 3 :(得分:-2)

考虑采用以下方法覆盖所有情况

protected List<object> ExecuteMultiQuery<A, B, C, D, E, F, G, H, I, J>(string procedureName, DynamicParameters param = null)

    {
        List<object> result = new List<object>();
        using (var connection = new SqlConnection(ConnectionManager.ConnectionString))
        {
            try
            {
                connection.Open();
                using (var multi = connection.QueryMultiple(procedureName, param, commandType: CommandType.StoredProcedure, commandTimeout: 120))
                {
                    var varA = multi.Read<A>();
                    if (varA != null) { result.Add(varA.ToList()); }
                    var varB = multi.Read<B>();
                    if (varB != null) { result.Add(varB.ToList()); }
                    var varC = multi.Read<C>();
                    if (varC != null) { result.Add(varC.ToList()); }
                    var varD = multi.Read<D>();
                    if (varD != null) { result.Add(varD.ToList()); }
                    var varE = multi.Read<E>();
                    if (varE != null) { result.Add(varE.ToList()); }
                    var varF = multi.Read<F>();
                    if (varF != null) { result.Add(varF.ToList()); }
                    var varG = multi.Read<G>();
                    if (varG != null) { result.Add(varG.ToList()); }
                    var varH = multi.Read<H>();
                    if (varH != null) { result.Add(varH.ToList()); }
                    var varI = multi.Read<I>();
                    if (varI != null) { result.Add(varI.ToList()); }
                    var varJ = multi.Read<J>();
                    if (varJ != null) { result.Add(varJ.ToList()); }

                    //if (varA != null) { result.Add(varA.ToList()); }
                    //if (resultSets > 1) { result.Add(multi.Read<B>().ToList()); }
                    //if (resultSets > 2) { result.Add(multi.Read<C>().ToList()); }
                    //if (resultSets > 3) { result.Add(multi.Read<D>().ToList()); }
                    //if (resultSets > 4) { result.Add(multi.Read<E>().ToList()); }
                    //if (resultSets > 5) { result.Add(multi.Read<F>().ToList()); }
                    //if (resultSets > 6) { result.Add(multi.Read<G>().ToList()); }
                    //if (resultSets > 7) { result.Add(multi.Read<H>().ToList()); }
                    //if (resultSets > 8) { result.Add(multi.Read<I>().ToList()); }
                    //if (resultSets > 9) { result.Add(multi.Read<J>().ToList()); }    
                    return result;
                }
            }
            catch (System.Exception e)
            {
                string message = e.Message;
            }
        }

        return result;
    }