Unique属性是没有变化的,数据重复是因为merge的时候已经违反了你原来定义的约束.如果你把这个约束定义在PrimaryKey上,就不会用问题.你可以看看下面我写的代码:
using System;
using System.Data;namespace CSDNConsoleCS
{ class Test
{
public static void Main()
{
DataSet dsOne = new DataSet();
DataSet dsTwo = new DataSet(); DataTable tblOne = dsOne.Tables.Add("MergeTest");
tblOne.Columns.Add("ID",Type.GetType("System.Int32"));
tblOne.Columns.Add("Name",Type.GetType("System.String"));
tblOne.PrimaryKey = new DataColumn[]{tblOne.Columns["ID"]}; DataTable tblTwo = dsTwo.Tables.Add("MergeTest");
tblTwo.Columns.Add("ID",Type.GetType("System.Int32"));
tblTwo.Columns.Add("Name",Type.GetType("System.String"));
tblTwo.PrimaryKey = new DataColumn[]{tblTwo.Columns["ID"]};
DataRow row ;
row = tblOne.NewRow();
row["ID"] = 1;
row["Name"] = "First";
tblOne.Rows.Add(row); row = tblTwo.NewRow();
row["ID"] = 1;
row["Name"] = "First";
tblTwo.Rows.Add(row);
try
{
dsOne.Merge(dsTwo);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString() + System.Environment.NewLine);
}
finally
{ foreach(DataRow myRow in tblOne.Rows)
{
foreach(DataColumn col in tblOne.Columns)
Console.Write(myRow[col].ToString() + "\t");
Console.WriteLine();
}
Console.ReadLine();
}
} }
}