Excel导入Sql

 string path = File2.Value;

            string execelConnectionStr = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + path + "'" + ";Extended Properties=Excel 8.0";

            using (OleDbConnection conn = new OleDbConnection(execelConnectionStr))
            {
                OleDbCommand cmd = new OleDbCommand("select 编号, 姓名,密码,权限 FROM [Sheet1$]", conn);
                conn.Open();

                using (OleDbDataReader dr = cmd.ExecuteReader())
                {
                    // SQL Server Connection String 
                    string sqlConnectionString = "server=.;uid=sa;pwd=xudongsheng19861207/;database=zhuanhuandatabase";

                    // Bulk Copy to

 SQL Server 
                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
                    {
                        bulkCopy.DestinationTableName = "zhuanhuanbiao";
                        bulkCopy.WriteToServer(dr);
                    }
                }
            }
            Response.Write("Update ModelTable Completed."); 


这是个比较简单方法

 protected void Button1_Click(object sender, EventArgs e)
        {//批量添加学生信息
            string constr = "server=.;uid=sa;pwd=xudongsheng19861207/;database=zhuanhuandatabase";
            SqlConnection conn = new SqlConnection(constr); //链接数据库
          conn.Open();
         try
         {
            string fileurl = typename(this.FileUpload1);//调用typename方法取得excel文件路径
            DataSet ds = new DataSet();//取得数据集
            ds = xsldata(fileurl);
            int errorcount = 0;//记录错误信息条数
            int insertcount = 0;//记录插入成功条数
            int updatecount = 0;//记录更新信息条数
             for (int i = 0; i < ds.Tables[0].Rows.Count; i++)


           {
                string stuid = ds.Tables[0].Rows[i][0].ToString();
                string stuname = ds.Tables[0].Rows[i][1].ToString();
                string stusex = ds.Tables[0].Rows[i][2].ToString();
                string zhuanye = ds.Tables[0].Rows[i][3].ToString();
                string classname = ds.Tables[0].Rows[i][4].ToString();
                if (stuid != "" && stuname != "" && stusex != "" && zhuanye != "" && classname != "")
                {
                                  SqlCommand selectcmd = new SqlCommand("select count(*) from student  where zhuanye='" + zhuanye + "'and classname='" + classname + "'", conn);
                    int count = Convert.ToInt3

                     }
                        else
                        {
                            SqlCommand insertcmd= new SqlCommand("insert into student   values('" + stuid + "','" + stuname + "','" + stusex + "','" + zhuanye + "','" + classname+ "')", conn);
                            insertcmd.ExecuteNonQuery();
                            insertcount++;
                        }
                    }
                    else
                    {
                        Response.Write("<script language='javascript'>alert('专业或班级信息有错!导入失败!请检查!');</script>");
                        break;  

                     }
                }
                else
      

         {
                    errorcount++;
                }
            }
            Response.Write("<script language='javascript'>alert('" + insertcount + "条数据导入成功!" + updatecount + "条数据更新成功!" + errorcount + "条数据部分信息为空没有导入!');</script>");
        }
        catch (Exception ee)
        {
            Response.Write("<script language='javascript'>alert('导入失败!');</script>");
        }
        finally
        {
            conn.Close();
        }    
    }


 

 

//把EXCEL文件上传到服务器并返回文件路径

private String typename(FileUpload fileloads)
    {
        string fullfilename =fileloads.PostedFile.FileName;
        string filename = fullfilename.Subs

tring(fullfilename.LastIndexOf("\\") + 1);
        string type = fullfilename.Substring(fullfilename.LastIndexOf(".") + 1);
        string murl = "";
        if (type == "xls")
        {
            fileloads.PostedFile.SaveAs(Server.MapPath("excel") + "\\" + filename);
            murl = (Server.MapPath("excel") + "\\" + filename).ToString();
        }
        else
        {
            Response.Write("<script language='javascript'>alert('导入文件格式不对!');</script>");

        }
        return murl;
    }

  //把excel数据读入dataset返回l数据集

 private DataSet xsldata(string filepath)
    {
        string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended P

2(selectcmd.ExecuteScalar());
                    if (count > 0)
                    {
                        SqlCommand selectcmd2 = new SqlCommand("select count(*) from student where  stuid='" + stuid + "'", conn);
                        int count2 = Convert.ToInt32(selectcmd2.ExecuteScalar());
                        if (count2 > 0)
                        {
                            SqlCommand updatecmd = new SqlCommand("update student set  stuname='" + stuname + "',stusex='" + stusex + "',zhuanye='" + zhuanye + "',classname='" + classname + "'  where stuid='" + stuid + "'", conn);
                            updatecmd.ExecuteNonQuery();
                            updatecount++;
  

roperties='Excel 8.0;IMEX=1'";
        System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
        string strCom = "SELECT * FROM [Sheet1$]";
        Conn.Open();
        System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
        DataSet ds = new DataSet();
        myCommand.Fill(ds, "[Sheet1$]");
        Conn.Close();
        return ds;
    } 



 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值