如何将一个DataTable追加到另一个DataTable ?

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

I would like to append one DataTable to another DataTable. I see the DataTable class has two methods; "Load(IDataReader)" and "Merge(DataTable)". From the documentation, both appear to 'merge' the incoming data with the existing DataTable if rows exist. I will be doing the merge in a data access layer.

我想把一个DataTable附加到另一个DataTable。我看到DataTable类有两个方法;“负载(IDataReader)”和“合并(数据表)”。从文档中可以看出,如果存在行,那么它们都可以“合并”传入的数据和现有的DataTable。我将在数据访问层中进行合并。

I could could usa an IDataReader and use the Load method to merge the DataTables. Or I could load a DataSet using the IDataReader, get the DataTable from the DataSet, and then use the Merge method to merge the DataTables.

我可以使用IDataReader并使用Load方法来合并数据。或者我可以使用IDataReader加载数据集,从数据集中获取数据表,然后使用Merge方法来合并数据。

I was wondering if someone could tell me which is the proper method to use?

我想知道是否有人能告诉我哪种方法是合适的?

Alternatively, let me know if you have a different suggestion on how to accomplish this task.

或者,如果您对如何完成这项任务有不同的建议,请告诉我。

4 个解决方案

#1


41  

Merge takes a DataTable, Load requires an IDataReader - so depending on what your data layer gives you access to, use the required method. My understanding is that Load will internally call Merge, but not 100% sure about that.

Merge具有一个DataTable, Load需要一个IDataReader——因此,根据您的数据层提供的访问权限,使用所需的方法。我的理解是Load将在内部调用Merge,但不是100%确定。

If you have two DataTables, use Merge.

如果您有两个datatable,请使用Merge。

#2


41  

The datatype in the same columns name must be equals.

相同列名称中的数据类型必须是相等的。

dataTable1.Merge(dataTable2);

After that the result is:

之后的结果是:

dataTable1 = dataTable1 + dataTable2

dataTable1 = dataTable1 + dataTable2

#3


16  

You could let your DataAdapter do the work. DataAdapter.Fill(DataTable) will append your new rows to any existing rows in DataTable.

您可以让您的DataAdapter执行此工作。填充(DataTable)将把新行附加到DataTable中的任何现有行。

#4


5  

Add two datasets containing datatables, now it will merge as required

添加两个包含datatables的数据集,现在它将根据需要合并。

DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();

DataTable dt1 = new DataTable();
dt1.Columns.Add(new DataColumn("Column1", typeof(System.String)));

DataRow newSelRow1 = dt1.NewRow();
newSelRow1["Column1"] = "Select";
dt1.Rows.Add(newSelRow1);

DataTable dt2 = new DataTable();
dt2.Columns.Add(new DataColumn("Column1", typeof(System.String)));

DataRow newSelRow2 = dt1.NewRow();
newSelRow2["Column1"] = "DataRow1Data";  // Data
dt2.Rows.Add(newSelRow2);

ds1.Tables.Add(dt1);
ds2.Tables.Add(dt2);

ds1.Tables[0].Merge(ds2.Tables[0]);

Now ds1 will have the merged data

现在ds1将拥有合并的数据

#1


41  

Merge takes a DataTable, Load requires an IDataReader - so depending on what your data layer gives you access to, use the required method. My understanding is that Load will internally call Merge, but not 100% sure about that.

Merge具有一个DataTable, Load需要一个IDataReader——因此,根据您的数据层提供的访问权限,使用所需的方法。我的理解是Load将在内部调用Merge,但不是100%确定。

If you have two DataTables, use Merge.

如果您有两个datatable,请使用Merge。

#2


41  

The datatype in the same columns name must be equals.

相同列名称中的数据类型必须是相等的。

dataTable1.Merge(dataTable2);

After that the result is:

之后的结果是:

dataTable1 = dataTable1 + dataTable2

dataTable1 = dataTable1 + dataTable2

#3


16  

You could let your DataAdapter do the work. DataAdapter.Fill(DataTable) will append your new rows to any existing rows in DataTable.

您可以让您的DataAdapter执行此工作。填充(DataTable)将把新行附加到DataTable中的任何现有行。

#4


5  

Add two datasets containing datatables, now it will merge as required

添加两个包含datatables的数据集,现在它将根据需要合并。

DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();

DataTable dt1 = new DataTable();
dt1.Columns.Add(new DataColumn("Column1", typeof(System.String)));

DataRow newSelRow1 = dt1.NewRow();
newSelRow1["Column1"] = "Select";
dt1.Rows.Add(newSelRow1);

DataTable dt2 = new DataTable();
dt2.Columns.Add(new DataColumn("Column1", typeof(System.String)));

DataRow newSelRow2 = dt1.NewRow();
newSelRow2["Column1"] = "DataRow1Data";  // Data
dt2.Rows.Add(newSelRow2);

ds1.Tables.Add(dt1);
ds2.Tables.Add(dt2);

ds1.Tables[0].Merge(ds2.Tables[0]);

Now ds1 will have the merged data

现在ds1将拥有合并的数据