简体   繁体   中英

can I add the same DataTable with same name to a DataSet multiple times?

i have 12 datatables in the list Like "A,B,C,D,A,B,C,D,A,B,C,D" I want to add these datatables to dataset and then write to Xml but it shows the exception:'A DataTable named 'A' already belongs to this DataSet

    For Each dataTable In dtList
        dsxml.Tables.Add(dataTable.Copy())
    Next
    dsxml.WriteXml(path)

need help how to achieve this.

Adding tablename to datatables will solve the issue. I had created a small tested code snippet for your understanding..

''Sample class to hold datatables and their names
    Class cls_dtlist
        Public Property TableName As String
        Public Property Tabledata As DataTable
    End Class

    Sub Main()

        ''Dummy Tables
        Dim A As DataTable = New DataTable()
        Dim B As DataTable = New DataTable()
        Dim C As DataTable = New DataTable()
        Dim D As DataTable = New DataTable()
        Dim E As DataTable = New DataTable()
        Dim F As DataTable = New DataTable()


        Dim pathofXml As String = "your path"


        Dim dtlist As List(Of cls_dtlist) = New List(Of cls_dtlist)

        ''Adding datatables to class and then list
        dtlist.Add(New cls_dtlist With {.TableName = "A", .Tabledata = A})
        dtlist.Add(New cls_dtlist With {.TableName = "B", .Tabledata = B})
        dtlist.Add(New cls_dtlist With {.TableName = "C", .Tabledata = C})
        dtlist.Add(New cls_dtlist With {.TableName = "D", .Tabledata = D})
        dtlist.Add(New cls_dtlist With {.TableName = "E", .Tabledata = E})
        dtlist.Add(New cls_dtlist With {.TableName = "F", .Tabledata = F})


        Dim dsxml As DataSet = New DataSet()

        For Each dataTable In dtlist

            dataTable.TableName = dataTable.TableName

            dsxml.Tables.Add(dataTable.Tabledata.Copy())
        Next

        dsxml.WriteXml(pathofXml )

    End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM