如何使用 DataReader 填充 DataTable
我想使用 DataReader 填充 DataTable。
我创建了这样的对象
SqlDataReader dr = cmd.ExecuteReader();
if(dr.HasRows)
{
}
I want to fill DataTable using DataReader.
I have created object like this
SqlDataReader dr = cmd.ExecuteReader();
if(dr.HasRows)
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想要的只是用于报告或网络的只读数据表,请尝试以下操作:
应得的信用: http://www.dotnetcurry.com/showarticle.aspx?ID=143
If all you want is a ReadOnly DataTable for reporting or web, try this:
Credit where it's due: http://www.dotnetcurry.com/showarticle.aspx?ID=143
DataTable.load() 可用于通用方法。
DataTable.load() can be used for a generic approach.
您可以从
SqlDataReader dr
获取架构表以获取列名称,将名称保存到List
并将它们添加为新上的列DataTable
,然后使用dr
上的索引使用列表中的名称填充该DataTable
:显然,您需要
try...catch。 ..finally
阻止这一切来处理异常和处置连接,并使用finally
之后的最后一个条件。我发现这有助于查明何时有结果,并避免了在没有结果时失败的 dt.Load(dr) 问题。ds.Fill(adapter)
也好不了多少,因为当我尝试使用SELECT * FROM MyTable
获取包含 97 列和大约 80 行的表格时,它失败了。对我来说,只有上面的代码能够在所有场景中工作。最初发布于 sarathkumar 的从数据读取器填充数据表。我提供了摘要,对其进行了压缩,添加了空检查并分配它是否为空值,并将表添加到了 DataSet 并在末尾添加了 DataSet 条件。
注意:对于那些使用
OracleDataReader
的用户,我发现如果您的NCLOB
或CLOB
字段在您正在阅读的表/结果集。我发现如果我通过查看索引i
来检查该列并执行dr.GetOracleClob(i)
而不是dr[i]
,我不再收到异常。请参阅 EF + ODP 的答案.NET + CLOB = Value Cannot be Null - Parameter name: byteArray? 并且我在if (!dr.IsDBNull[i])
时在上面的代码中添加了此条件。同样,如果您有一个空的Decimal
字段,我必须使用dr.GetFloat(i);
检查它,因为dr.GetOracleDecimal(i); 都没有。
和dr.GetDecimal(i);
似乎正确地适应了空值。You can get the Schema Table from your
SqlDataReader dr
to get the column names, save the names to aList<string>
and add them as columns on a newDataTable
, then fill thatDataTable
using indexing ondr
with the names from the list:Obviously you'd need your
try...catch...finally
block around it all to handle exceptions and disposing your connection, and use the last condition after thefinally
. I found this helpful in order to handle finding out when I had results or not, and avoided issues withdt.Load(dr)
that was failing when there were no results.ds.Fill(adapter)
wasn't much better, as it failed when I tried to grab a table of 97 columns and about 80 rows withSELECT * FROM MyTable
. Only the code above managed to work in all scenarios, for me.Originally posted on Populate data table from data reader by sarathkumar. I provided the summary, condensed it, added the null checks and assigning if it's a null value, and added the table to a
DataSet
and added theDataSet
condition at the end.NOTE: For those using
OracleDataReader
, I found out that you can experience an error if you have anNCLOB
orCLOB
field that is null in the table/results set that you are reading. I found if I checked for that column by looking at the indexi
and diddr.GetOracleClob(i)
instead ofdr[i]
, I stopped getting the exception. See answer at EF + ODP.NET + CLOB = Value Cannot be Null - Parameter name: byteArray? and I added this condition in the code above whenif (!dr.IsDBNull[i])
. Similarly, if you have a nullDecimal
field, I had to check it withdr.GetFloat(i);
, since neitherdr.GetOracleDecimal(i);
anddr.GetDecimal(i);
seemed to correctly accommodate for a null value.要填充
DataSet
,您可以使用以下内容:To fill a
DataSet
, you can use something like: