把excel文件数据导入到sql数据库表中

 /// <summary>
        /// 导入一个excel文件
        /// </summary>
        /// <param name="FileName">要导入的文件名</param>
        /// <returns>返回一个集合</returns>
        private DataSet GetDataSet(string FileName)
        {
            //连接字符串
            string oledbConStr = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source ='" + FileName + "';Extended Properties=Excel 8.0";
            //sql语句
            string oleSql = "select * from [Sheet1$]";
            //创建con对象链接数据库
            OleDbConnection oledbCon = new OleDbConnection(oledbConStr);
            //创建dataAdapter填充数据集
            OleDbDataAdapter oledbAdapter = new OleDbDataAdapter(oleSql, oledbCon);
            //创建DataSet对象
            DataSet ExcelDs = new DataSet();
            //打开链接
            oledbCon.Open();
            //填充数据集
            oledbAdapter.Fill(ExcelDs);
            //关闭链接
            oledbCon.Close();
            return ExcelDs;
        }
        /// <summary>
        /// 将excel中的考勤信息导入数据库
        /// </summary>
        private void AddSourceFromExcel()
        {
            DataSet excelDs = new DataSet();
            //要执行的sql语句,暂时无.这里采用Stringbuilder类,因为接下来字符串连接操作比较多
            StringBuilder sqlBuilder = new StringBuilder();
            //创建连接
            string ConStr = "Data Source=.;Initial Catalog=Employee;Integrated Security=SSPI";
            SqlConnection con = new SqlConnection(ConStr);
            //选择一个打开的文件
            OpenFileDialog openFile = new OpenFileDialog();
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                excelDs = GetDataSet(openFile.FileName);
            }
            //创建命令
            SqlCommand com = con.CreateCommand();
            //打开连接
            con.Open();
            //捕抓错误
            try
            {

                //循环把数据插入到sqlServer中
                for (int i = 0; i < excelDs.Tables[0].Rows.Count; i++)
                {
                    sqlBuilder.Append("insert into Checking (Month ,Day ,EmpId) values ( '");
                    for (int j = 0; j < 2; j++)
                    {
                        sqlBuilder.Append(excelDs.Tables[0].Rows[i].ItemArray[j].ToString() + "','");

                    }
                    sqlBuilder.Append(excelDs.Tables[0].Rows[i].ItemArray[2].ToString() + " ')");

                    //执行sql语句
                    string sql = sqlBuilder.ToString();
                    com.CommandText = sql;
                    com.ExecuteNonQuery();

                    sqlBuilder.Remove(0, sqlBuilder.Length);
                }
                MessageBox.Show("考勤导入成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("导入考勤过程中发生错误!/n错误提示:" + ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                con.Close();
            }
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值