从数据集循环多个表

时间:2013-05-20 04:18:23

标签: asp.net vb.net

我的数据集中有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

代码说明 该函数用于传入数据集并将数据集转换为字符串 我想要实现的是将多个数据表转换为字符串,但我只能在上面的代码中循环一个表,我该怎么做才能循环多个表? =(

1 个答案:

答案 0 :(得分: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中get XML而不是将DataSet转换为字符串。我不确定你转换为sting的确切要求是什么,但XML将是沟通数据的好方法。

xmlString =lpDataSet.GetXml()