protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("col");
for (int i = 5; i >0;i=i-2)
{
DataRow row = dt.NewRow();
row["col"] = "车" + i;
dt.Rows.Add(row);
}
DataTable dt1 = new DataTable();
dt1.Columns.Add("col1");
for (int i = 1; i <= 4; i++)
{
DataRow row = dt1.NewRow();
row["col1"] = "车" + i;
dt1.Rows.Add(row);
}
DataTable resultTable = cf(dt,dt1);
Response.Write("重复:");
for (int i = 0; i < resultTable.Rows.Count; i++)
Response.Write(resultTable.Rows[i]["col"].ToString());
Response.Write("<br/>");
resultTable = bcfbyd1tod2(dt, dt1);
Response.Write("不重复 d1tod2:");
for (int i = 0; i < resultTable.Rows.Count; i++)
Response.Write(resultTable.Rows[i]["col"].ToString());
Response.Write("<br/>");
resultTable = bcfbyd2tod1(dt, dt1);
Response.Write("不重复 d2tod1:");
for (int i = 0; i < resultTable.Rows.Count; i++)
Response.Write(resultTable.Rows[i]["col"].ToString());
Response.Write("<br/>");
}
/// <summary>
/// 取重复
/// </summary>
/// <param name="dt"></param>
/// <param name="dt1"></param>
/// <returns></returns>
private DataTable cf(DataTable dt, DataTable dt1)
{
DataTable myTable = dt.Clone();
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow[] sRow= dt1.Select("col1='"+dt.Rows[i]["col"].ToString()+"'");
if (sRow.Length > 0)
{
DataRow row= myTable.NewRow();
row["col"] = dt.Rows[i]["col"].ToString();
myTable.Rows.Add(row);
}
}
return myTable;
}
/// <summary>
/// 不重复d1tod2
/// </summary>
/// <param name="dt"></param>
/// <param name="dt1"></param>
/// <returns></returns>
private DataTable bcfbyd1tod2(DataTable dt, DataTable dt1)
{
DataTable myTable = dt.Clone();
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow[] sRow = dt1.Select("col1='" + dt.Rows[i]["col"].ToString() + "'");
if (sRow.Length == 0)
{
DataRow row = myTable.NewRow();
row["col"] = dt.Rows[i]["col"].ToString();
myTable.Rows.Add(row);
}
}
return myTable;
}
/// <summary>
/// 不重复d1tod2
/// </summary>
/// <param name="dt"></param>
/// <param name="dt1"></param>
/// <returns></returns>
private DataTable bcfbyd2tod1(DataTable dt, DataTable dt1)
{
DataTable myTable = dt.Clone();
for (int i = 0; i < dt1.Rows.Count; i++)
{
DataRow[] sRow = dt.Select("col='" + dt1.Rows[i]["col1"].ToString() + "'");
if (sRow.Length == 0)
{
DataRow row = myTable.NewRow();
row["col"] = dt1.Rows[i]["col1"].ToString();
myTable.Rows.Add(row);
}
}
return myTable;
}
两个 DataTable 读取重复数据,dataTable1与dataTable2不同
最新推荐文章于 2017-09-21 11:54:51 发布