-1

我正在创建一个页面以将数据从 excel 上传到数据库,它在本地系统中工作正常而不是在服务器中。请帮我解决这个错误..这是我的部分代码..

      if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))
        {
            path = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
            SaveLocation = Server.MapPath("~/Temp") + "\\" + path;
            path = "Temp/" + path;
            FileUpload1.PostedFile.SaveAs(SaveLocation);

            //Import from Excel to database
            System.Data.OleDb.OleDbDataAdapter MyCommand = default(System.Data.OleDb.OleDbDataAdapter);
            System.Data.OleDb.OleDbConnection MyConnection = default(System.Data.OleDb.OleDbConnection);
            MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0; " + "data source=" + SaveLocation + "; " + "Extended Properties=Excel 12.0;");
            MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
            DS = new System.Data.DataSet();
            MyCommand.Fill(DS);
            DataTable dt = DS.Tables[0];
            if (DS == null)
            {
                lblstatus.Text = "Problem occurred during uploading process... ";
            }
            else
            {
                foreach (DataRow row in dt.Rows)
                {
                    DataSet5TableAdapters.sp_import_emp_generalTableAdapter TA = new DataSet5TableAdapters.sp_import_emp_generalTableAdapter();
                    TA.GetData(Convert.ToString(row["fldempid"]), Convert.ToString(row["fldlastname"]), Convert.ToString(row["fldfirstname"]), Convert.ToString(row["fldnickname"]),
                        Convert.ToString(row["fldDOB"]), Convert.ToString(row["fldmarital"]), Convert.ToString(row["fldgender"]), Convert.ToString(row["fldsmoker"]),
                        Convert.ToString(row["fldpic"]), Convert.ToString(row["fldaddress1"]), Convert.ToString(row["fldaddress2"]), Convert.ToString(row["fldcity"]),

                        Convert.ToString(row["fldstate"]), Convert.ToString(row["fldmobile"]), Convert.ToString(row["fldtele"]), Convert.ToString(row["fldwemail"]),
                        Convert.ToString(row["fldoemail"]), Convert.ToString(row["fldzip"]), Convert.ToString(row["fldtitle"]), Convert.ToString(row["fldjoindate"]),
                        Convert.ToString(row["fldspecification"]), Convert.ToString(row["fldcompany"]), Convert.ToString(row["fldgroup"]), Convert.ToString(row["flddept"]),
                        Convert.ToDouble(row["fldCTC"]), Convert.ToDouble(row["flddiv"]), Convert.ToDouble(row["fldbasic"]), Convert.ToDouble(row["fldhra"]),
                        Convert.ToDouble(row["fldconveyance"]), Convert.ToDouble(row["fldPF"]), Convert.ToDouble(row["fldesi"]), Convert.ToDouble(row["fldPT"]),

                        Convert.ToDouble(row["fldTDS"]), Convert.ToString(row["fldcafeteria"]), Convert.ToDouble(row["fldbasicvalue"]), Convert.ToDouble(row["fldhravalue"]),
                        Convert.ToDouble(row["fldconvalue"]), Convert.ToDouble(row["fldpfvalue"]), Convert.ToDouble(row["fldesivalue"]), Convert.ToDouble(row["fldptvalue"]),
                        Convert.ToDouble(row["fldtdsvalue"]), Convert.ToDouble(row["fldGrosspay"]), Convert.ToDouble(row["fldgrossearn"]), Convert.ToDouble(row["fldtotaldeduction"]),
                        Convert.ToDouble(row["fldnetpay"]), Convert.ToString(row["fldaccount"]), Convert.ToString(row["fldemployer"]), Convert.ToString(row["fldjobtitle"]),
                        Convert.ToString(row["fldstart"]), Convert.ToString(row["fldend"]), Convert.ToString(row["fldtotalexp"]), Convert.ToString(row["fldcomment"]),
                        Convert.ToString(row["fldedu"]), Convert.ToString(row["fldmajor"]), Convert.ToInt32(row["fldpassedout"]), Convert.ToString(row["fldskill"]),
                        Convert.ToString(row["fldexp"]), Convert.ToString(row["fldcomments"]), Convert.ToString(row["fldlanguage"]), Convert.ToString(row["fldfluency"]),
                        Convert.ToString(row["fldstatus"]));

                }

                System.Threading.Thread.Sleep(5000);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "onload", "<script language='javascript'>alert('Data Updated Successfully...');</script>", false);
                lblstatus.Text = "Data updated successfully..";
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "onload", "<script language='javascript'>alert('Please select a file to upload');</script>", false);

        }

    }
    catch (Exception e1)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "onload", "<script language='javascript'>alert('" + e1.Message + "');</script>", false);
    }    

请帮我解决这个错误....

4

1 回答 1

2

如果将数据添加到本地数据库,则检查上述代码毫无意义。当您上传 Excel 文件时,您的服务器数据库连接或权限问题一定有问题。

确保您对上传文件的文件夹具有权限:

SaveLocation = Server.MapPath("~/Temp") + "\\" + path;
于 2012-04-06T12:24:18.400 回答