我希望将Execl 表里面的数据导入到数据库
当我代码在本地的时候 导入到sql数据库成功了 可是 当我发布到服务器上面以后 失败了 求高手
我的代码是下面的
string fileName = FileUpload1.FileName; string fPath = Server.MapPath("Upload//") + fileName;//文件保存的路径,服务器上的绝对路径 string fPaths = Server.MapPath("~/Upload/") + fileName; string type = fileName.Substring(fileName.LastIndexOf(".") + 1);//获取文件后缀名 //Label1.Text = type; if (type != "xls") Response.Write("<script language=\"javascript\">alert('只接受上传Excel文件!')</script>"); else { string thro = ""; int drcount = 0; int zcount = 0; try { //上传文件 thro = "保存路径出错"; FileUpload1.SaveAs(fPath); ////////////////将文件内容导入SQL/////////////////////////////////// //将Excel内容读入数据集中 string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + fPath + "';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, "Test"); GridView1.DataSource = myDs.Tables["Test"]; GridView1.DataBind(); //从excel文件获得数据后,插入记录到SQL Server的数据表 thro = "数据库链接有错"; SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnString"].ToString());
DataTable dataTable1 = new DataTable();
SqlDataAdapter sqlDA1 = new SqlDataAdapter(@"SELECT * FROM Test", sqlCon);
SqlCommandBuilder sqlCB1 = new SqlCommandBuilder(sqlDA1); thro = "数据库链接有错2"; sqlDA1.Fill(dataTable1);
//字段名 和Execl必须一样 foreach (DataRow dataRow in myDs.Tables["Test"].Rows) { DataRow dataRow1 = dataTable1.NewRow();
CRM_CourseBLL coursebll = new CRM_CourseBLL();
dataRow1["Photo"] = dataRow[0]; string a = dataRow[0].ToString(); //判断 如果该电话存在 匹配成功 if (coursebll.retuntcount("CRM_Course", "ID", " where " + " C_Mobile = '" + a + "' and C_cost <> 1") > 0) { dataRow1["state"] = 0; dataRow1["drtime"] = DateTime.Now;
dataTable1.Rows.Add(dataRow1); drcount = drcount + 1; } zcount = zcount + 1; } thro = "数据填充报错"; sqlDA1.Update(dataTable1);
cnnxls.Close(); } catch { ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + fPath + thro + "');", true); daoruurl.InnerHtml = fPath + "---" + fPaths + thro; return; }
最后给出来的失败提示 是 保存路径出错
可是 我在服务器上面查看了 那个路径中 那个文件是存在的 求高手
添加回答
举报
0/150
提交
取消