cs代码
1 DataTable dtExcel = new ExcelHelper().ExcelImportToDataTable(ServerFilePath, version);
2
3 IDictionary dic = new Dictionary<string, string>();
4 //查询HareWare表信息
5 DataTable dtHw = hardWare.GetAllHardWareList();
6
7 foreach(DataRow dr in dtHw.Rows){
8 if (dr["MAC1"] != "")
9 {
10 dic.Add(dr["MAC1"].ToString(), dr["AssetID"].ToString());
11 }
12 }
13
14 IList<string> ilToDel = new List<string>();
15 //查询不匹配的MAC对应的信息
16 foreach (DataRow dr in dtExcel.Rows)
17 {
18 if (dic.Contains(dr[0].ToString()))
19 {
20 //获取要删除的资产编号
21 string assetId = dic[dr[0].ToString()].ToString();
22 ilToDel.Add(assetId);
23 }
24
25 }
26
27 //查询硬件信息
28 dtDB = new AMS_MAC().GetMACInfo();
29 if (dtDB.Rows.Count > 0)
30 {
31 for (int k = 0; k < dtDB.Rows.Count; k++)
32 {
33 if (ilToDel.Contains(dtDB.Rows[k]["AssetID"].ToString()))
34 {
35 dtDB.Rows[k].Delete();
36 }
37 }
38 }
39 dtDB.AcceptChanges();
DataTable增加一列并赋值:
代码
DataTable dt = new ExcelHelper().ExcelImportToDataTable(ServerFilePath, version);
//ds.Tables.Add(dt);
Session["dtExcel"] = dt;
//dt.Columns.Add("操作记录", Type.GetType("System.String"));
//DataRow dr = dt.NewRow()
//dt.Rows.Add(new object[] { "导入失败", "" });
dt.Columns.Add("result", typeof(string));
foreach (DataRow dr in dt.Rows)
{
if (InsertData(dt))
{
dr["result"] = "success";
}
else
{
dr["result"] = "failed";
}
}
/// <summary>
/// DataTable内容存到数据库
/// </summary>
/// <returns></returns>
protected bool InsertData(DataTable inputdt)
{
DataTable dtExcel = ((DataTable)Session["dtExcel"]).Copy();
foreach (DataRow dr in dtExcel.Rows)
{
excel_report.excel_column1 = dr[0].ToString();
excel_report.excel_column2 = dr[1].ToString();
}
if (excelReport.Add(excel_report))
{
return true;
}
else
{
return false;
}