您的位置:首页 > 编程语言 > ASP

asp.net中excel导入ms sql数据库

2008-06-05 15:39 239 查看
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();}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: