简体   繁体   中英

The Microsoft Jet database engine could not find the object

I'm trying to upload an Excel file to a SQL database and was working fine on my computer, but once I uploaded to the server it's giving me this error:

Exception Details: System.Data.OleDb.OleDbException: The Microsoft Jet database engine could not find the object 'C:\\Windows\\SysWOW64\\inetsrv\\Book1.xls'. Make sure the object exists and that you spell its name and the path name correctly.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Here is my code:

  protected void Button1_Click(object sender, EventArgs e) { string excelConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HRD=YES;IMEX=1'", FileUpload1.PostedFile.FileName); using (OleDbConnection connection = new OleDbConnection(excelConnectionString)) { OleDbCommand command = new OleDbCommand(("Select * FROM [Sheet1$]"), connection); connection.Open(); using (DbDataReader dr = command.ExecuteReader()) { using (SqlBulkCopy bulkCopy = new SqlBulkCopy("Data Source=WSCJTCSQ1;Initial Catalog=TestDB;Persist Security Info=True;User ID=test;Password=test")) { bulkCopy.DestinationTableName = "CoaTest"; bulkCopy.ColumnMappings.Add("First Name", "fName"); bulkCopy.ColumnMappings.Add("Last Name", "lName"); bulkCopy.ColumnMappings.Add("Agency", "agency"); bulkCopy.WriteToServer(dr); } } } Label1.ForeColor = System.Drawing.Color.Red; Label1.Text = "Successfully Uploaded The New Roster"; } 
string file = string.Format("{0}\\{1}", Request.PhysicalApplicationPath,
Guid.NewGuid().ToString().Replace("-",string.Empty));

FileUpload1.SaveAs(file)

Now in your import code

string excelConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source={0};Extended Properties='Excel 8.0;HRD=YES;IMEX=1'", file);

You need to save the file on the server before you connect to it, something like:

string sServerFilespec = ("C:\\ServerFolder\\" + System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName));
FileUpload1.PostedFile.FileName.SaveAs(sServerFilespec);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM