protected void change_ServerClick(object sender, EventArgs e) { string mystring="Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filedata.Value +";Extended Properties=Excel 8.0"; OleDbConnection cnnxls = new OleDbConnection (mystring); OleDbDataAdapter myDa =new OleDbDataAdapter("select * from [Sheet1$]",cnnxls); DataSet myDs =new DataSet(); myDa.Fill(myDs); if(myDs.Tables[0].Rows.Count > 0) { string strSql = ""; string CnnString = "Provider=SQLOLEDB;database=DRUGS_CMS;server=(local);uid=sa;pwd=sa"; OleDbConnection conn =new OleDbConnection(CnnString); conn.Open (); OleDbCommand myCmd =null; for(int i=0; i<myDs.Tables[0].Rows.Count; i++) { strSql = "insert into CMS_DRUS values ('"; strSql += myDs.Tables[0].Rows[i].ItemArray[0].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i].ItemArray[1].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i].ItemArray[2].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i].ItemArray[3].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i].ItemArray[4].ToString() + "', '"; strSql += int.Parse(myDs.Tables[0].Rows[i].ItemArray[5].ToString()) + "', '"; strSql += Decimal.Parse(myDs.Tables[0].Rows[i].ItemArray[6].ToString()) + "', '"; strSql += myDs.Tables[0].Rows[i].ItemArray[7].ToString() + "', '"; strSql += DateTime.Parse(myDs.Tables[0].Rows[i].ItemArray[8].ToString()) + "', '"; strSql += int.Parse(myDs.Tables[0].Rows[i].ItemArray[9].ToString()) + "', '"; strSql += Decimal.Parse(myDs.Tables[0].Rows[i].ItemArray[10].ToString()) + "', '"; strSql += myDs.Tables[0].Rows[i].ItemArray[11].ToString() + "', '"; strSql += DateTime.Parse(myDs.Tables[0].Rows[i].ItemArray[12].ToString()) + "', '"; strSql += int.Parse(myDs.Tables[0].Rows[i].ItemArray[13].ToString()) + "', '"; strSql += DateTime.Parse(myDs.Tables[0].Rows[i].ItemArray[14].ToString()) + "', '"; strSql += int.Parse(myDs.Tables[0].Rows[i].ItemArray[15].ToString()) + "')"; try { myCmd=new OleDbCommand(strSql,conn); myCmd.ExecuteNonQuery(); Response.Write("<script language=javascript>alert('数据导入成功.');</script>"); } catch(Exception m_Error) { Response.Write(m_Error.Message.ToString()); } } conn.Close();}
asp.net中excel导入ms sql数据库
最新推荐文章于 2020-11-11 11:41:15 发布