This question already has an answer here:
How to handle multiple ResultSets, each with multiple Rows? IDataReader.NextResult() ending Read()

(1个答案)


3年前关闭。




我有一个SP,我试图从中返回2个结果集,并且在我的.cs文件中,我尝试这样的操作:
dr = cmd.ExecuteReader();
while (dr.Read())
{
  RegistrationDetails regDetails = new RegistrationDetails()
  {
    FName = dr["FName"].ToString(),
    LName = dr["LName"].ToString(),
    MName = dr["MName"].ToString(),
    EntityName = dr["EntityName"].ToString(),// in 2nd result set
    Percentage = dr["Percentage"].ToString()// in 2nd result set
   };
}

但是我得到一个:

最佳答案

这里有一个有关如何使用数据读取器处理多个结果集的示例

static void RetrieveMultipleResults(SqlConnection connection)
{
    using (connection)
    {
        SqlCommand command = new SqlCommand(
          "SELECT CategoryID, CategoryName FROM dbo.Categories;" +
          "SELECT EmployeeID, LastName FROM dbo.Employees",
          connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        do
        {
            Console.WriteLine("\t{0}\t{1}", reader.GetName(0),
                reader.GetName(1));

            while (reader.Read())
            {
                Console.WriteLine("\t{0}\t{1}", reader.GetInt32(0),
                    reader.GetString(1));
            }
        }
        while (reader.NextResult());
    }
}

从多个数据集中检索数据的关键是使用reader.NextResult

关于c# - 如何从SqlDataReader中读取多个结果集? [复制],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19448899/

10-13 08:13