gpt4 book ai didi

vb.net - 在 DataRow 中循环 DataColumn

转载 作者:行者123 更新时间:2023-12-04 18:23:29 28 4
gpt4 key购买 nike

我正在将数据表内容转换为字符串,假设数据集有 12 行和 8 列。
最后,字典包含 12 行或 96 列。下面的代码有什么问题,如何重置行?

蒂亚。

        Dim ds As DataSet = ClsDB.GetDataSet(sql)

If Not ds Is Nothing AndAlso ds.Tables.Count > 0 Then
Dim row As New List(Of KeyValuePair(Of String, String))
Dim dict As New List(Of KeyValuePair(Of Integer, Object))
Dim dt As DataTable = ds.Tables(0)
Dim i As Integer = 0

For Each dr As DataRow In dt.Rows
For Each dc As DataColumn In dt.Columns
row.Add(New KeyValuePair(Of String, String)(dc.ColumnName, IIf(Not IsDBNull(dr.Item(dc.ColumnName)), dr.Item(dc.ColumnName), "")))
Next
dict.Add(New KeyValuePair(Of Integer, Object)(i, row))
i += i
Next

Return New JavaScriptSerializer().Serialize(dict)
Else
Return "No Data"
End If

最佳答案

您应该重新初始化 row对于每个 dr ,其实你不需要声明rowdr环形。

For Each dr As DataRow In dt.Rows
Dim row As New List(Of KeyValuePair(Of String, String)) // <<-
For Each dc As DataColumn In dt.Columns
row.Add(...
Next
dict.Add(...
Next

关于vb.net - 在 DataRow 中循环 DataColumn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10187998/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com