DataTable数据导入DataBase

EXcel---->DataTable--->DataBase

      /// <summary>
        /// Excel数据-->DataTable导入DataBase
        /// </summary>
        /// <param name="filePath"></param>
        public void GetExcelData(string filePath)
        {
            ExcelHelper excelHelper = new ExcelHelper(filePath);
            //Excel转化成DataTable
            DataTable dt = excelHelper.ExcelToDataTable();
            List<OleDbParameter> listEmp = new List<OleDbParameter>();
            string sql = "insert into T_Employee(EmpId,Name,CardNo,Indate,Flag) values(@EmpId,@Name,@CardNo,@Indate,@Flag)";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                listEmp.Add(new OleDbParameter("@EmpId", dt.Rows[i][1].ToString()));
                listEmp.Add(new OleDbParameter("@Name", dt.Rows[i][2].ToString()));
                listEmp.Add(new OleDbParameter("@CardNo", dt.Rows[i][3].ToString()));
                listEmp.Add(new OleDbParameter("@Indate", Convert.ToDateTime(dt.Rows[i][4])));
                listEmp.Add(new OleDbParameter("@Flag", dt.Rows[i][5].ToString() == "" ? true : false));
                //插入T_Employee数据
                AccessHelper.ExecuteNonQuery(sql, listEmp.ToArray());
                //清理,好多次插入
                listEmp.Clear();
            }
        }
View Code

 

posted @ 2018-03-20 13:40  森林长  阅读(323)  评论(0编辑  收藏  举报