Excel数据导入到MsSQL数据库中

    protected void Button3_Click(object sender, EventArgs e)
    {
       
            string conn = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source ='D:\\ccd.xls';Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
            OleDbConnection thisconnection = new OleDbConnection(conn);
            thisconnection.Open();
            string Sql = "select * from [Sheet1$]";
            OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, thisconnection);
            DataSet ds = new DataSet();
            mycommand.Fill(ds, "[Sheet1$]");

            SqlConnection scon = new SqlConnection("server=.;uid=sa;pwd=sa;database=Tex");
            scon.Open();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string strValues = "'" + ds.Tables[0].Rows[i][0].ToString() + "','" + ds.Tables[0].Rows[i][1].ToString() + "')";

                SqlCommand sc = new SqlCommand("insert Tex values(" + strValues, scon);
                sc.ExecuteNonQuery();
            }
            scon.Close();
            thisconnection.Close();
            Response.Write("插入成功");
    }

转自:http://www.cnblogs.com/zrr1210/archive/2009/04/02/1427818.html


另一方法:

    在MSSQL2005中运行这样一条SQL命令:

 SELECT * INTO mm FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0','Data Source=e:\mhp2g_armorData.xls;Extended Properties=Excel 8.0')Excel数据导入到MsSQL数据库中[SkillBase$]

其中的"mm"是要导入到的表(自动新建,事先不要存在)

datasourse后面是excel的地址

 

相关文章:

  • 2021-04-11
  • 2021-11-11
  • 2021-04-11
  • 2021-11-25
  • 2021-11-23
  • 2022-01-05
  • 2022-01-20
猜你喜欢
  • 2022-01-30
  • 2021-12-22
相关资源
相似解决方案