如何在数据表中追加一行?

时间:2022-09-17 11:38:11

I have a datatable say dt1( which keeps changing its inside a loop). I have another datatable say dt2( initially its null). Now I have to append the Rows of dt1 in dt2. I tried using Merge(), but the previous rows of dt2 are vanishing. Any idea How to do this ??

我有一个datatable, dt1(它在循环中不断地改变它)。我有另一个datatable, dt2(初始值为null)。现在我需要在dt2中添加dt1的行。我尝试使用Merge(),但是之前的dt2行正在消失。你知道怎么做吗?

6 个解决方案

#1


1  

You are clearing the dt2 table everytime the loop runs. Try this:

每次循环运行时,都要清理dt2表。试试这个:

DataTable dt1 = null; DataTable dt2 = null;

for (int i = 0; i < dt3.Rows.Count; i++) 
{

    // here  "strSQL" is build which changes on the "i" value                  

    dt1 = GetDataTable(strSQL); // this returns a table with single Row

    if(dt2 == null) 
    {
       dt2 = dt1.Clone();
    }

    dt2.Merge(dt1,true);
}

Also, if the query restriction based on i is applied to a primary key column you can use

此外,如果基于i的查询限制应用于主键列,则可以使用

dt2.ImportRow(dt1.Rows[0]);

instead of

而不是

dt2.Merge(dt1,true);

#2


1  

Use the ImportRow method, like this:

使用ImportRow方法,如下所示:

var table2 = new DataTable();

foreach(DataRow row in table1.Rows)
    table2.ImportRow(row);

#3


1  

Based on your code, I see that you're using dt2 = dt1.Clone(); That's wiping all the contents in dt2, so you're only adding the current contents of dt1 to dt2.

根据您的代码,我看到您正在使用dt2 = dt1.Clone();这是在清除dt2中的所有内容,所以你只需要将dt1的当前内容添加到dt2中。

Instead of cloning you should just merge the contents of dt1 to dt2.

与其克隆,不如将dt1的内容合并到dt2。

#4


1  

Another derivative to João Angelo's answer would be to initialize dt2 ahead of time and then you can remove the null check

另一个对Joao Angelo的解释是提前初始化dt2,然后你可以删除空检查。

DataTable dt1 = null; DataTable dt2 = new DataTable();

for (int i = 0; i < dt3.Rows.Count; i++) 
{

    // here  "strSQL" is build which changes on the "i" value                  

    dt1 = GetDataTable(strSQL); // this returns a table with single Row

    dt2.Merge(dt1,true);
}

#5


0  

What about this:

这个:

  DataRow[] rows = new DataRow[dt1.Rows.Count];
  dt1.Rows.CopyTo(rows, 0);
  foreach (DataRow row in rows)
  {
    dt2.Rows.Add(row);
  }

#6


0  

i'm assuming your tables have the same structure

假设你们的表格有相同的结构

 foreach (DataRow row in dt1.Rows)
     dt2.Rows.Add(row.ItemArray);

#1


1  

You are clearing the dt2 table everytime the loop runs. Try this:

每次循环运行时,都要清理dt2表。试试这个:

DataTable dt1 = null; DataTable dt2 = null;

for (int i = 0; i < dt3.Rows.Count; i++) 
{

    // here  "strSQL" is build which changes on the "i" value                  

    dt1 = GetDataTable(strSQL); // this returns a table with single Row

    if(dt2 == null) 
    {
       dt2 = dt1.Clone();
    }

    dt2.Merge(dt1,true);
}

Also, if the query restriction based on i is applied to a primary key column you can use

此外,如果基于i的查询限制应用于主键列,则可以使用

dt2.ImportRow(dt1.Rows[0]);

instead of

而不是

dt2.Merge(dt1,true);

#2


1  

Use the ImportRow method, like this:

使用ImportRow方法,如下所示:

var table2 = new DataTable();

foreach(DataRow row in table1.Rows)
    table2.ImportRow(row);

#3


1  

Based on your code, I see that you're using dt2 = dt1.Clone(); That's wiping all the contents in dt2, so you're only adding the current contents of dt1 to dt2.

根据您的代码,我看到您正在使用dt2 = dt1.Clone();这是在清除dt2中的所有内容,所以你只需要将dt1的当前内容添加到dt2中。

Instead of cloning you should just merge the contents of dt1 to dt2.

与其克隆,不如将dt1的内容合并到dt2。

#4


1  

Another derivative to João Angelo's answer would be to initialize dt2 ahead of time and then you can remove the null check

另一个对Joao Angelo的解释是提前初始化dt2,然后你可以删除空检查。

DataTable dt1 = null; DataTable dt2 = new DataTable();

for (int i = 0; i < dt3.Rows.Count; i++) 
{

    // here  "strSQL" is build which changes on the "i" value                  

    dt1 = GetDataTable(strSQL); // this returns a table with single Row

    dt2.Merge(dt1,true);
}

#5


0  

What about this:

这个:

  DataRow[] rows = new DataRow[dt1.Rows.Count];
  dt1.Rows.CopyTo(rows, 0);
  foreach (DataRow row in rows)
  {
    dt2.Rows.Add(row);
  }

#6


0  

i'm assuming your tables have the same structure

假设你们的表格有相同的结构

 foreach (DataRow row in dt1.Rows)
     dt2.Rows.Add(row.ItemArray);