如何使用 DbDataRecord.GetValues 填充 DataRow?
使用 VB9,我尝试使用多个存储过程调用的结果填充 DataTable。我正在尝试做的工作看起来像:
For Each record In New SqlCommand("exec getResults", conn).ExecuteReader
Dim dr As DataRow = dt.NewRow
record.GetValues(dr.ItemArray)
dt.Rows.Add(dr)
Next
看起来 DataRow.ItemArray 和 DbDataRecord.GetValues 应该整齐地插入在一起 - 并且它确实编译...但是当它运行时,DataRow填充空值而不是 SqlDataReader 的结果。
我可以遍历 DbDataRecord 中的字段并将它们一一插入到 DataRow 中,如下所示:
For Each record In New SqlCommand("exec getResults", conn).ExecuteReader
Dim dr As DataRow = dt.NewRow
For fieldLoop = 0 To 9
dr.Item(fieldLoop) = record(fieldLoop)
Next
dt.Rows.Add(dr)
Next
但似乎不需要这样做。我认为 MSDN 对这一问题没有帮助。
那么,我是不是完全找错了树呢?或者如果我只是有一个小错误,那是什么?
Using VB9, I'm trying to populate a DataTable with the results from several stored procedure calls. What I'm trying to make work looks like:
For Each record In New SqlCommand("exec getResults", conn).ExecuteReader
Dim dr As DataRow = dt.NewRow
record.GetValues(dr.ItemArray)
dt.Rows.Add(dr)
Next
It seems like the DataRow.ItemArray and DbDataRecord.GetValues ought to plug together neatly -- and it does compile... But when it runs, the DataRow is filled with null values instead of the results from the SqlDataReader.
I can loop across the fields in the DbDataRecord and insert them one by one into the DataRow like:
For Each record In New SqlCommand("exec getResults", conn).ExecuteReader
Dim dr As DataRow = dt.NewRow
For fieldLoop = 0 To 9
dr.Item(fieldLoop) = record(fieldLoop)
Next
dt.Rows.Add(dr)
Next
but doesn't seem like that should be needed. I'm not finding MSDN to be helpful on this one.
So, am I barking up the wrong tree entirely? Or if I just have a little error, what is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 datatable.load 方法并传入 datareader 对象。 http://msdn.microsoft.com/en-我们/library/system.data.datatable.load.aspx
use the
datatable.load
method and pass in the datareader object. http://msdn.microsoft.com/en-us/library/system.data.datatable.load.aspx