SQLDataReader未读取存储过程返回的第二个表

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

我正在使用以下代码使用datareader读取并填充两个C#对象。但我看不到第二张桌子。

using (var myConnection = new SqlConnection(ConnectionString))
{
     var sqlCommand = "usp_GetFileListforPurging";
     var cmd = new SqlCommand(sqlCommand, myConnection) { CommandType = CommandType.StoredProcedure };
     cmd.CommandTimeout = Timeout == 0 ? 30 : Timeout * 30;
     myConnection.Open();
     using (var reader = cmd.ExecuteReader())
     {
          _tableAllSet.Load(reader); //read's the first table
          reader.NextResult(); //But this is returning false, although my SP is returning two tables
          _tableTrueSet.Load(reader);
     }
     myConnection.Close();
}

下面是SP返回的数据片段

enter image description here

c# sqldatareader
1个回答
2
投票

[DataTable.Load]已经使阅读器进步了,基本上是:

        if (!reader.IsClosed && !reader.NextResult())
        {
            reader.Close();
        }

citation from reference source

所以:在使用NextResult时不要自己叫Load,因为那样会导致第二个网格被跳过。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.