程序员的部落

导航

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("插入成功");
    }

posted on 2009-04-02 10:15  程序员的部落  阅读(366)  评论(0编辑  收藏  举报