首页 > 代码库 > DataTable的Merge使用
DataTable的Merge使用
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1{ class Program { private static void PrintValues(DataTable table) { string label = table.TableName; Console.WriteLine("TableName: " + table.TableName); foreach (DataColumn column in table.Columns) { Console.Write("\t " + column.ColumnName); } Console.WriteLine(); foreach (DataRow row in table.Rows) { foreach (DataColumn column in table.Columns) { Console.Write("\t |{0}|", row[column]); } Console.WriteLine(); } } static void Main(string[] args) { DataTable dtWs = new DataTable("dtWs"); //DataColumn dcWsWId = new DataColumn("WID", Type.GetType("System.Int32"));//“System.Data.DataException”类型的未经处理的异常在 System.Data.dll 中发生其他信息: < target >.WID 和<source>.WID 擁有衝突的屬性: DataType 屬性不符。 DataColumn dcWsWId = new DataColumn("WID", Type.GetType("System.String")); DataColumn dcWsFId = new DataColumn("FID", Type.GetType("System.String")); DataColumn dcBNId = new DataColumn("ADD", Type.GetType("System.String")); dtWs.Columns.Add(dcWsWId); dtWs.Columns.Add(dcWsFId); dtWs.Columns.Add(dcBNId); dtWs.PrimaryKey = new DataColumn[] { dcWsWId, dcWsFId }; for (int i = 0; i < 5; i++) { DataRow drNew = dtWs.NewRow(); drNew["WID"] = i; drNew["FID"] = ""; drNew["ADD"] = "BBBB" + i; dtWs.Rows.Add(drNew); } PrintValues(dtWs); DataTable dtQry = new DataTable("dtQry"); DataColumn dcWId = new DataColumn("WID", Type.GetType("System.String")); DataColumn dcFId = new DataColumn("FID", Type.GetType("System.String")); DataColumn dcNId = new DataColumn("Name", Type.GetType("System.String")); dtQry.Columns.Add(dcWId); dtQry.Columns.Add(dcFId); dtQry.Columns.Add(dcNId); dtQry.PrimaryKey = new DataColumn[] { dcWId, dcFId }; for (int i = 3; i < 8; i++) { DataRow drNew = dtQry.NewRow(); drNew["WID"] = i; drNew["FID"] = ""; drNew["Name"] = "AAAA" + i; dtQry.Rows.Add(drNew); } PrintValues(dtQry); dtQry.Merge(dtWs, true, MissingSchemaAction.AddWithKey); PrintValues(dtQry); dtQry.Merge(dtWs, false, MissingSchemaAction.AddWithKey); PrintValues(dtQry); Console.Read(); } }}
執行結果:
DataTable的Merge使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。