合并:
DataTable pros=xxx;
DataTable pstar=yyy;
//将两张DataTable合成一张
foreach (DataRow dr in pstar.Rows)
{
pros.ImportRow(dr);
}
DataTable设置主键,并判断重复
DataTable allpros =xxx;
单列设为主键:
//设置第某列为主键
allpros.PrimaryKey = new DataColumn[] { pros.Columns[0]};
foreach (DataRow dr in pros.Rows)
{
//排除相同id,相同类型的程序
if (!allpros.Rows.Contains(dr[0]}))
allpros.ImportRow(dr);
}
多列设为主键
allpros.PrimaryKey = new DataColumn[] { pros.Columns[0], pros.Columns[1] };
if (!allpros.Rows.Contains(new object[2]{dr[0],dr[1]}))
allpros.ImportRow(dr);
时间: 2024-12-15 08:01:31