[英]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: 我正在尝试将
Excel
文件上传到SQL
数据库,并且在我的计算机上工作正常,但是一旦我将其上传到服务器,就会出现此错误:
Exception Details: System.Data.OleDb.OleDbException: The Microsoft Jet database engine could not find the object 'C:\\Windows\\SysWOW64\\inetsrv\\Book1.xls'.
异常详细信息:System.Data.OleDb.OleDbException:Microsoft Jet数据库引擎找不到对象'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.
当前Web请求的执行期间生成了未处理的异常。 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.