English 中文(简体)
数据已经属于另一个数据表
原标题:DataTable already belongs to another DataSet

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))
最佳回答

与其他答复一样,你所看到的错误是,因为你重新尝试在数据Set中添加数据,已经是另一个数据Set的一部分。

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

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

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

If you only want the structure of the DataTable, call Clone instead.

dtCopy = dataTable.Clone()
问题回答

接受的答案是好的。 克隆应始终是最后的选择。

这里是在不承担克隆技术的代价的情况下解决问题。

        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);

Try calling this method:

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

这将产生一份<代码>的可发射的副本,并将其分配给目标<代码>。 数据Set:

ds.Tables.Add(dt)

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

您可以将可追溯至XML的数据编成序列,然后将数据输入你的目标数据表。

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

http://msdn.microsoft.com/en-us/library/system.data.table.copy.aspx”rel=“nofollow”>DataTable.Copy(),如果它没有分类数据Set的话。 这种方法创建了同一表格的新案例,因此,它不属于任何数据表:

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

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

我有同样的问题,我用<代码>Remove解决了这一问题。 我认为,your Code 可以:

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.

<<>My >,code

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();
        }

每当我点击《纽特州<代码><>丁顿Save时,“可测量性已经属于另一个数据Set”的信息就出现。 在撰写行文代码myDataSet.Tables.Remove(myTable);/本工程后,申请开始运转,没有问题,现在我可以多点击子,不失去myTable的价值,也没有错误信息。

我希望这能够有所帮助。

我发现,我一看一看,我希望它能够帮助我。

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

即便是“可出口”是新的,但如果指以前装货的物理表,数据可继承以前的组合。

简单的做法是将表格合并如下。

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

dtCopy = 数据可用性。

ds.Tables.Add(dtCopy)

www.un.org/Depts/DGACM/index_spanish.htm 我们 优化守则,如。

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





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签