版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!
恰饭广告
注意:SourceDt为源表,field为去重字段(可增加)
private DataTable GetDistinctSelf(DataTable SourceDt, string field) { int j = SourceDt.Rows.Count; if (j > 1) { int k = j - 2; int i = 1; while (i <= k) { DataRow dr = SourceDt.Rows[i]; string isbn = dr[field].ToString(); DataRow[] rows = SourceDt.Select(string.Format("{0}='{1}'", field, isbn)); if (rows.Length > 1) { SourceDt.Rows.RemoveAt(i); //存在重复就移除 k = k - 1; } else { i++; } } } return SourceDt; }
取差集
private DataTable ExcelNo() { DataTable excel = excelToDt();//读取excel的dt DataTable except = new DataTable(); //接受差集的dt DataTable dt2 = shelvesbll.isGoodsShelves(regId); //读取数据库的dt //接受差集dt的列名 except.Columns.Add("id", typeof(string)); except.Columns.Add("货架名称", typeof(string)); except.Columns.Add("地区ID", typeof(string)); DataRowCollection count = excel.Rows; foreach (DataRow row in count)//遍历excel数据集 { try { string goodsName = row[1].ToString(); DataRow[] rows = dt2.Select(string.Format("shelvesName='{0}'", goodsName)); //查询货架名称是否重复 if (rows.Length == 0)//判断如果DataRow.Length为0,即该行excel数据不存在于数据库中,就插入到差集中 { except.Rows.Add(row[0], row[1], row[2]); } } catch (Exception ex) { Response.Write(ex); Response.End(); } } return except; }
原文链接:https://www.idaobin.com/archives/1969.html
让我恰个饭吧.ヘ( ̄ω ̄ヘ)
恰饭广告