C#中有两个数据表 TableA 和TableB如下TableA如下
---------------------------
ID CID
111
222 编号2
333 编号3
444
---------------------------
TableB
ID CID
111 编号1
444 编号4
---------------------------请问如何根据相同的ID 将两张表合并成一张下列各式的表----------
ID CID
111 编号1
222 编号2
333 编号3
444 编号4
---------------------------
ID CID
111
222 编号2
333 编号3
444
---------------------------
TableB
ID CID
111 编号1
444 编号4
---------------------------请问如何根据相同的ID 将两张表合并成一张下列各式的表----------
ID CID
111 编号1
222 编号2
333 编号3
444 编号4
忘了补充了 不是数据库 是C# winform程序里面
参考http://topic.csdn.net/u/20120830/17/51cfbe08-cd98-41cb-a052-be75663e598e.html
dt1.Columns.Add("ID");
dt1.Columns.Add("CID");
dt1.Rows.Add(111, "");
dt1.Rows.Add(222, "编号2");
dt1.Rows.Add(333, "编号3");
dt1.Rows.Add(444, "");
DataTable dt2 = dt1.Clone();
dt2.Rows.Add(111, "编号1");
dt2.Rows.Add(444, "编号4"); dt1.Merge(dt2);
for (int i = 0; i < dt1.Rows.Count; i++)
{
if (dt1.Rows[i]["CID"] == string.Empty)
dt1.Rows.RemoveAt(i--);
}
dt1.DefaultView.Sort = "ID";
DataTable dt = dt1.DefaultView.ToTable();
DataTable dt2 = GetDataTable();
dt1.Merge(dt2);//合并
DataView dv = dt1.DefaultView;
DataTable dtDistinct = dv.ToTable(true,new string[]{"ID","CID"});//去除重复行
DataTable dt1 = new DataTable();
dt1.Columns.Add("ID");
dt1.Columns.Add("CID");
dt1.Rows.Add(111, "");
dt1.Rows.Add(222, "编号2");
dt1.Rows.Add(333, "编号3");
dt1.Rows.Add(444, "");
DataTable dt2 = dt1.Clone();
dt2.Rows.Add(111, "编号1");
dt2.Rows.Add(444, "编号4");
dt1.Merge(dt2); foreach (DataRow row in dt1.Rows)
{
if (row["CID"] == "")
row["CID"] = dt2.Select("ID=" + row["ID"])[0]["CID"];
}
DataTable dt = dt1.DefaultView.ToTable(true);
DataTable dt1 = new DataTable();
dt1.Columns.Add("ID");
dt1.Columns.Add("CID");
dt1.Rows.Add(111, "");
dt1.Rows.Add(222, "编号2");
dt1.Rows.Add(333, "编号3");
dt1.Rows.Add(444, "");
DataTable dt2 = dt1.Clone();
dt2.Rows.Add(111, "编号1");
dt2.Rows.Add(444, "编号4"); foreach (DataRow row in dt1.Rows)
{
if (row["CID"] == "")
row["CID"] = dt2.Select("ID=" + row["ID"])[0]["CID"];
}