2

我正在将数据表内容转换为字符串,假设数据集有 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
4

1 回答 1

4

您应该row为each 重新初始化dr,实际上您不需要在循环row之外声明。dr

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
于 2012-04-17T13:52:41.370 回答