.net DataTable 已经属于另一个 DataSet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8830901/
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-03 16:11:08  来源:igfitidea点击:

DataTable already belongs to another DataSet

.netvb.netdatatable

提问by kbvishnu

This error is occuring while adding one datatable from a dataset to another ."DataTable already belongs to another DataSet."

将一个数据表从一个数据集中添加到另一个数据表时发生此错误。“数据表已属于另一个数据集。”

dsformulaValues.Tables.Add(m_DataAccess.GetFormulaValues
(dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0))

回答by Jay Riggs

Like the other responses point out, the error you're seeing is because the DataTable you're attempting to add to a DataSet is already a part of a different DataSet.

就像其他回复指出的那样,您看到的错误是因为您尝试添加到 DataSet 的 DataTable 已经是不同 DataSet 的一部分。

One solution is to Copythe DataTable and assign the copy to the other DataSet.

一种解决方案是复制DataTable 并将副本分配给另一个 DataSet。

dtCopy = dataTable.Copy()
ds.Tables.Add(dtCopy)

The copied DataTable will have the structure and data of the copied DataTable.

复制的 DataTable 将具有复制的 DataTable 的结构和数据。

If you only want the structure of the DataTable, call Cloneinstead.

如果您只需要 DataTable 的结构,请改为调用Clone

dtCopy = dataTable.Clone()

回答by Nathaniel Layton

The accepted answer isn't very good. Cloning should always be a last option.

接受的答案不是很好。克隆应该永远是最后的选择。

Here's a way around the problem without incurring the overhead of cloning.

这是解决问题的方法,而不会产生克隆开销。

        DataSet ds = GetData1();

        DataSet ds2 = GetData2();

        //Assuming you know you've got good data

            DataTable dt = ds2.Tables[0];
            ds2.Tables.Remove(dt);
            dt.TableName = "PortedTable";//you may need to change the table name to prevent conflicts
            ds.Tables.Add(dt);

回答by Abbas

Try calling this method:

尝试调用此方法:

DataTable dt = dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0).Clone()

DataTable dt = dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0).Clone()

This will create a copy of the DataTableand assign it to the target DataSet:

这将创建 的副本并将其DataTable分配给目标DataSet

ds.Tables.Add(dt)

ds.Tables.Add(dt)

回答by GMarco1989

I had the same problem and I solved it using Remove. In my opinion, your codecould be this:

我遇到了同样的问题,我使用Remove. 在我看来,您的代码可能是这样的:

dsformulaValues.Tables.Add(m_DataAccess.GetFormulaValues
(dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0))

dsformulaValues.Tables.Remove(//I'm not sure to understand your code, so read this code line as only an input for your stuff. Please, consider my code below for more understanding.

Myworking codewas like this:

我的工作代码是这样的:

DataTable myTable = new DataTable();

private void Save()
{
    DataSet myDataSet = new DataSet();
    myDataSet.Tables.Add(myTable);
    myDataSet.Tables.Remove(myTable);//This works
    myDataSet.WriteXml("myTable.xml");
}

private void buttonSave_Click(object sender, EventArgs e)
        {
          Save();
        }

Every time I clicked the button buttonSave, the message “DataTable already belongs to another DataSet" appeared. After writing the line code myDataSet.Tables.Remove(myTable);//This worksthe application started running without problems and now I can click the button more times, without losing the value of myTableand without the error message.

每次单击按钮时buttonSave,都会出现“DataTable 已属于另一个数据集”消息。编写行代码后 myDataSet.Tables.Remove(myTable);//This works,应用程序开始正常运行,现在我可以多次单击按钮,不会丢失 的值,myTable也不会出现错误消息.

I hope this can help.

我希望这会有所帮助。

回答by Juan Garcia

I found one turn around I hope it can help

我找到了一个转身希望它可以帮助

_DataTable.TableName = _TableName
If _DataTable.DataSet IsNot Nothing Then
    _DataSet = _DataTable.DataSet
Else
    _DataSet = New DataSet
    _DataSet.Tables.Add(_DataTable)
End If

Somehow even if _DataTable is new, but if it refers to a previous loaded physical table it becomes that DataTable inheriting the previous configuration.

不知何故,即使 _DataTable 是新的,但如果它引用以前加载的物理表,它就会成为继承以前配置的 DataTable。

回答by Natarajan Ganapathi

The simple way is just merge the table as follows.

简单的方法就是合并表格如下。

dsformulaValues.Merge(m_DataAccess.GetFormulaValues(dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0))

dsformulaValues.Merge(m_DataAccess.GetFormulaValues(dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0))

回答by Amal P S

dtCopy = dataTable.Copy() ds.Tables.Add(dtCopy)

dtCopy = dataTable.Copy() ds.Tables.Add(dtCopy)

This Solution can also written in a single line

这个解决方案也可以写成一行

ds.Tables.Add(dataTable.Copy());

ds.Tables.Add(dataTable.Copy());

回答by zmbq

I guess this means the DataTable belongs to another DataSet...

我想这意味着 DataTable 属于另一个 DataSet ...

You can serialze the DataTable to XML, then deserialize it into your target DataSet.

您可以将 DataTable 序列化为 XML,然后将其反序列化为您的目标 DataSet。

回答by Gavin Fang

I think u should create a new DataTable and import the struct and data to the new one.

我认为你应该创建一个新的 DataTable 并将结构和数据导入新的。

回答by pistipanko

Try to copy the table with DataTable.Copy()method in case it's not a typed DataSet. This method creates a new instance of the same table so it not gonna belong to any DataSet:

尝试使用DataTable.Copy()方法复制表,以防它不是类型化的 DataSet。此方法创建同一个表的新实例,因此它不属于任何数据集:

dim newTable as DataTable = oldTable.Copy() dim dv as DataView = newTable.DefaultView

将 newTable 调暗为 DataTable = oldTable.Copy() 将 dv 调暗为 DataView = newTable.DefaultView

dsformulaValues.Tables.Add(m_DataAccess.GetFormulaValues(dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0))

dsformulaValues.Tables.Add(m_DataAccess.GetFormulaValues(dv.ToTable.DefaultView.ToTable(False, strSelectedCols)).Tables(0))