vb.net 如何将现有的数据表放入数据集?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22184012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:27:08  来源:igfitidea点击:

How to put an existing DataTable into a DataSet?

vb.net

提问by Mox

That's just it. I have an existing DataTable, and I would like to make it a part of a existing DataSet. Any ideas on how to go about it? I tried a couple of things, but none of them work...

仅此而已。我有一个现有的 DataTable,我想让它成为现有 DataSet 的一部分。关于如何去做的任何想法?我尝试了几件事,但都没有奏效......

回答by Steve

The Dataset contains a collection of Tables, and, as with every collection, you could use the method Add to add your table to the dataset.
However there is one thing to be aware of. If your table is already part of another dataset (probably because you used the DataAdapter.Fill(DataSet) method), then you should remove the table from the previous dataset tables collection before adding it to the new one.

数据集包含一组表,并且与每个集合一样,您可以使用 Add 方法将您的表添加到数据集。
但是,有一点需要注意。如果您的表已经是另一个数据集的一部分(可能是因为您使用了 DataAdapter.Fill(DataSet) 方法),那么您应该在将它添加到新的数据集表集合之前从之前的数据集表集合中删除该表。

Dim dsNew = New DataSet()   ' The dataset where you want to add your table
Dim dt As DataTable = GetTable()   ' Get the table from your storage
Dim dsOld = dt.DataSet        ' Retrieve the DataSet where the table has been originally added
if dsOld IsNot Nothing Then  
    dsOld.Tables.Remove(dt.TableName)  ' Remove the table from its dataset tables collection
End If
dsNew.Tables.Add(dt)  ' Add to the destination dataset.

回答by Reed Copsey

You can use dataSet.Tables.Add(dataTable).

您可以使用dataSet.Tables.Add(dataTable).

See DataTableCollection.Addfor details.

有关详细信息,请参阅DataTableCollection.Add