简体   繁体   English

DataTable没有加载SqlDataReader

[英]DataTable not loading SqlDataReader

using (SqlConnection connection = new SqlConnection(connectionString))
    {
        using (SqlCommand command = new SqlCommand(sqlCommand, connection))
        {
            command.CommandType = CommandType.Text;
            connection.Open();
            using (SqlDataReader reader = command.ExecuteReader())
            {
                DataTable datatable = new DataTable();
                datatable.Load(reader);

                return datatable;
            }
        }
    }

Running this code returns an empty DataTable. 运行此代码将返回一个空的DataTable。 However, looping through reader.Read() and printing to the debug console shows that the reader has data and it prints the expected data. 但是,通过reader.Read()循环并打印到调试控制台会显示读者有数据并打印预期的数据。 Also, When I expand the reader object during debugging, hasRows is true and the field count is right for the number of columns returned. 此外,当我在调试期间展开reader对象时,hasRows为true,并且字段计数适合返回的列数。

There was a similar post here Trouble loading SQL Data Reader data into DataTable but the answer essentially was, just don't use it, use a SqlDataAdapter . 在这里有一个类似的帖子麻烦加载SQL数据读取器数据到DataTable但答案基本上是,只是不要使用它,使用SqlDataAdapter I would prefer to use it and the DataTable has a load method that takes an IDataReader DataTable.Load(IDataReader) . 我更喜欢使用它, DataTable有一个带有IDataReader DataTable.Load(IDataReader)的加载方法。 I just don't know why the reader is working when I print it to the debug window but not when I load it into the DataTable . 我只是不知道为什么读者在我将它打印到调试窗口时工作,而不是在我将它加载到DataTable Am I just overlooking something? 我只是忽略了什么?

It turns out I was just overlooking something and this is actually not an issue at all. 事实证明我只是忽略了一些东西,这实际上根本不是问题。 The original code actually works fine. 原始代码实际上工作正常。 The preview of the DataTable object when debugging shows {} and looked empty to me. 调试时, DataTable对象的预览显示{}并且对我来说是空的。 在此输入图像描述

Then there was a property on the object called ExtendedProperties that had Count = 0 , which obviously isn't a row count but I just glossed over and got Count = 0 stuck in my head. 然后在对象上有一个名为ExtendedProperties的属性,其Count = 0 ,这显然不是行数,但我只是掩盖了并且Count = 0卡在了我脑海中。

在此输入图像描述

If you find yourself in the same situation, scroll down when hovering the object and expand the Rows and you should see your row count in there. 如果您发现自己处于相同的情况,请在悬停对象时向下滚动并展开Rows ,您应该在那里看到行数。 在此输入图像描述

I got duped on this one... sorry for the stupidity on my part and thanks for the help everyone. 我被骗了这个...对不起我的愚蠢,感谢大家的帮助。

Try doing this see if it works... 尝试这样看看它是否有效......

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlDataAdapter dap = new SqlDataAdapter(sqlCommand,connection);
    DataTable datatable = new DataTable();
    dap.Fill(datatable);
    return datatable;
}

Use SqlDataReader or DataAdapter , both will work. 使用SqlDataReaderDataAdapter ,两者都可以。 Write the return statement after closing the connection. 关闭连接后写入return语句。 Try if it works. 尝试它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM