0

我的数据集中有 2 个表,我想要实现的是将 2 个表项转换为字符串

Public Shared Function mtdCDsToStr(ByVal pDataSet As DataSet) As String
        Dim sDs As String
        Dim sb As New System.Text.StringBuilder


        Dim drRow As DataRow
        Dim dcColumn As DataColumn
        Dim dtTable As DataTable
        Dim x As Integer = 0

        For Each dtTable In pDataSet.Tables
            For Each drRow In pDataSet.Tables(x).Rows
                Dim colName(pDataSet.Tables(x).Columns.Count) As String
                Dim i As Integer = 0
                For Each dcColumn In pDataSet.Tables(0).Columns
                    colName(i) = dcColumn.ColumnName
                    sb.Append(colName(i) + "," + drRow(colName(i)).ToString + ",")
                    i += 1
                Next

                sb.Append("|")
            Next


            x += 1
            sb.Append("$")
        Next


        sDs = sb.ToString

        Return sDs
    End Function

代码解释 该功能是传入一个数据集并将数据集转换为字符串我试图实现的是将多个数据表转换为字符串但我只能在上面的代码中循环一个表,我应该怎么做才能循环多桌子?=(

4

1 回答 1

1

更改如下

For Each dtTable As DataTable In dataSet.Tables
    For Each dr As DataRow In dtTable.Rows
        For Each column As DataColumn In dtTable.Columns
            sb.Append(column.ColumnName + "," & dr(column.ColumnName).ToString() & ",")
        Next
        sb.Append("|")
    Next
    sb.Append("$")
Next

但是,您可以尝试从 DataSet获取 XML ,而不是将 DataSet 转换为字符串。我不确定您转换为 sting 的确切要求是什么,但 XML 将是传达数据的好方法。

xmlString =lpDataSet.GetXml() 
于 2013-05-20T04:25:39.453 回答