如何使用 DbDataRecord.GetValues 填充 DataRow?

发布于 2024-09-19 10:21:24 字数 758 浏览 13 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始于初秋 2024-09-26 10:21:24

使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文