C#中如何“联合”2个以上的数据表? [英] How to 'union' 2 or more DataTables in C#?
本文介绍了C#中如何“联合”2个以上的数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
两个表都具有相同的结构。
是否有任何内置函数或我们应该手动执行? / p>
解决方案
你最有可能是 DataTable.Merge 方法。
示例 :
private static void DemonstrateMergeTable()
{
DataTable table1 = new DataTable(Items) ;
//添加列
DataColumn idColumn = new DataColumn(id,typeof(System.Int32));
DataColumn itemColumn = new DataColumn(item,typeof(System.Int32));
table1.Columns.Add(idColumn);
table1.Columns.Add(itemColumn);
//设置主键列。
table1.PrimaryKey = new DataColumn [] {idColumn};
//为表添加RowChanged事件处理程序。
table1.RowChanged + = new
System.Data.DataRowChangeEventHandler(Row_Changed);
//添加十行。
DataRow行; (int i = 0; i< = 9; i ++)
{
row = table1.NewRow();
row [id] = i;
row [item] = i;
table1.Rows.Add(row);
}
//接受更改。
table1.AcceptChanges();
PrintValues(table1,原始值);
//创建与第一个数据表相同的第二个DataTable。
DataTable table2 = table1.Clone();
//将列添加到第二列,以使
// schemas不再匹配。
table2.Columns.Add(newColumn,typeof(System.String));
//添加三行。请注意,id列不能是原始表中现有行的
//。
row = table2.NewRow();
row [id] = 14;
row [item] = 774;
row [newColumn] =new column 1;
table2.Rows.Add(row);
row = table2.NewRow();
row [id] = 12;
row [item] = 555;
row [newColumn] =new column 2;
table2.Rows.Add(row);
row = table2.NewRow();
row [id] = 13;
row [item] = 665;
row [newColumn] =new column 3;
table2.Rows.Add(row);
//将table2合并到表1中。
Console.WriteLine(Merging);
table1.Merge(table2,false,MissingSchemaAction.Add);
PrintValues(table1,合并表1,添加模式);
}
private static void Row_Changed(object sender,
DataRowChangeEventArgs e)
{
Console.WriteLine(Row changed { } \t {1},e.Action,
e.Row.ItemArray [0]);
}
private static void PrintValues(DataTable表,字符串标签)
{
//显示提供的DataTable中的值:
Console.WriteLine (标签);
foreach(table.Rows中的DataRow行)
{
foreach(Table.Columns中的DataColumn col)
{
Console.Write(\t+ row [col] .ToString());
}
Console.WriteLine();
}
}
How to 'union' 2 or more DataTables in C#?
Both table has same structure.
Is there any build-in function or should we do manually?
解决方案
You are looking most likely for the DataTable.Merge method.
Example:
private static void DemonstrateMergeTable()
{
DataTable table1 = new DataTable("Items");
// Add columns
DataColumn idColumn = new DataColumn("id", typeof(System.Int32));
DataColumn itemColumn = new DataColumn("item", typeof(System.Int32));
table1.Columns.Add(idColumn);
table1.Columns.Add(itemColumn);
// Set the primary key column.
table1.PrimaryKey = new DataColumn[] { idColumn };
// Add RowChanged event handler for the table.
table1.RowChanged += new
System.Data.DataRowChangeEventHandler(Row_Changed);
// Add ten rows.
DataRow row;
for (int i = 0; i <= 9; i++)
{
row = table1.NewRow();
row["id"] = i;
row["item"] = i;
table1.Rows.Add(row);
}
// Accept changes.
table1.AcceptChanges();
PrintValues(table1, "Original values");
// Create a second DataTable identical to the first.
DataTable table2 = table1.Clone();
// Add column to the second column, so that the
// schemas no longer match.
table2.Columns.Add("newColumn", typeof(System.String));
// Add three rows. Note that the id column can't be the
// same as existing rows in the original table.
row = table2.NewRow();
row["id"] = 14;
row["item"] = 774;
row["newColumn"] = "new column 1";
table2.Rows.Add(row);
row = table2.NewRow();
row["id"] = 12;
row["item"] = 555;
row["newColumn"] = "new column 2";
table2.Rows.Add(row);
row = table2.NewRow();
row["id"] = 13;
row["item"] = 665;
row["newColumn"] = "new column 3";
table2.Rows.Add(row);
// Merge table2 into the table1.
Console.WriteLine("Merging");
table1.Merge(table2, false, MissingSchemaAction.Add);
PrintValues(table1, "Merged With table1, schema added");
}
private static void Row_Changed(object sender,
DataRowChangeEventArgs e)
{
Console.WriteLine("Row changed {0}\t{1}", e.Action,
e.Row.ItemArray[0]);
}
private static void PrintValues(DataTable table, string label)
{
// Display the values in the supplied DataTable:
Console.WriteLine(label);
foreach (DataRow row in table.Rows)
{
foreach (DataColumn col in table.Columns)
{
Console.Write("\t " + row[col].ToString());
}
Console.WriteLine();
}
}
这篇关于C#中如何“联合”2个以上的数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文