C# - DataReader无法返回多个结果集

时间:2014-06-26 10:55:58

标签: c# sqldatareader

我想做的事情与OP在这里所说的相似:

  

Multiples Table in DataReader

我试图将一个或多个SQL查询的结果存储到单独的DataTable中。上述问题方法的问题在于它仅适用于每个结果集中的离散值。最好使用DataTables Load(reader)方法将每个结果集存储在单个语句中,而不是迭代所有DataReader的列。

在下面的代码中,我有两个SQL查询(尽管这应该适用于任意数量的查询),其结果我尝试存储在临时数据表中,然后存储到数据表列表中。我的代码的问题是它只返回第一个查询的结果集,添加后续查询会抛出Reader is closed exception

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        using (SqlCommand command = new SqlCommand
               ("SELECT TOP 10 Column1, Column2 FROM Table1; SELECT TOP 10 Column1, Column2 FROM Table2", connection))
        {
            connection.Open();

            using (SqlDataReader reader = command.ExecuteReader())
            {
                    while (reader.Read())
                    {
                        tempTable.Reset();
                        tempTable.Load(reader);
                        dataTables.Add(tempTable);
                    }

                    if (reader.NextResult())
                    {
                        while (reader.Read())
                        {
                            tempTable.Reset();
                            tempTable.Load(reader);
                            dataTables.Add(tempTable);
                        }
                    }
            }
        }
    }

我已经挣扎了几个小时。我不确定DataTable / DataReader是否只是为我的用例设计得不好,或者是否有一些我在这里缺少的基本内容。

2 个答案:

答案 0 :(得分:5)

DataReader不适合我的需求,它的使用会产生冗长的代码,所以我使用了Daniel Kelly建议的DataAdapter。读取多个结果集的代码现在相对简单:

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        using (SqlCommand command = new SqlCommand
               ("SELECT TOP 10 Name, [Description] FROM Business; SELECT TOP 10 ConnectionId FROM Connection;SELECT TOP 10 Name FROM Business", connection))
        {
            connection.Open();

                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataSet set = new DataSet(); 
                adapter.SelectCommand = command;

                //Note here, that the adapter will create multiple tables for each result set
                adapter.Fill(set); 

                foreach (DataTable t in set.Tables)
                {
                    dataTables.Add(t);
                }

                dataTables.Add(tempTable);
        }
    } 

阅读完评论后,我发现我对reader.Read()和NextResult()的理解存在缺陷。此外,每次调用DataReader的加载方法时,读者的连接都将被关闭。

感谢大家的意见和建议。我不会接受我自己的答案,因为那样会自负。

答案 1 :(得分:4)

如果您更喜欢使用datatable.Load(阅读器),请尝试以下解决方案:

using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = new SqlCommand
                   ("SELECT TOP 10 Column1, Column2 FROM Table1; SELECT TOP 10 Column1, Column2 FROM Table2", connection))
            {
                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    bool haveresult = true;
                        while (haveresult)
                        {
                            DataTable tempTable = new DataTable();
                            tempTable.Load(reader);
                            dataTables.Add(tempTable);
                            try
                            {
                                reader.Read();
                            }
                            catch
                            {
                                haveresult = false;
                            }

                        }

                    }

            }
        }

您使用此方法是因为在读取器获得最后结果后,您无法控制何时关闭连接。请注意,这可用于您拥有的任何数量的查询

相关问题