• DataTable一点小操作


    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;
    }
  • 相关阅读:
    Django restfull规范
    Python3中的zip()
    关于负数的isdigit()判断
    Win7 x64安装Paramiko
    深度学习性能提高
    神经网络激励函数
    机器学习十大算法
    深度学习十大框架比较
    python 换行
    python sort() sorted() 与argsort()函数的区别
  • 原文地址:https://www.cnblogs.com/pfs1314/p/1681954.html
Copyright © 2020-2023  润新知