2013-02-20 112 views
2

我检查了上面的注释,但他们没有帮助。我使用VS2008作为数据库的ASP.Net和MS Access 2010。 我需要通过ASP网页将数据从excel上传到数据库。Microsoft Jet数据库引擎无法打开该文件。它已经由另一个用户专门打开,或者您需要查看其数据的权限。

但是我收到一个错误如下:

“Microsoft Jet数据库引擎无法打开文件 'C:\用户\ poonamj \文档\ Visual Studio 2008的\项目\智能工具\智能工具\上传\'它已经被另一个用户专门打开,或者您需要查看其数据的权限。“

using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using System.IO; 
using System.Collections.Generic; 
using System.Data.OleDb; 

namespace SmartTool 
{ 

    public partial class WebForm2 : System.Web.UI.Page 
    { 
     protected void UploadFile(object sender, EventArgs e) 
     { 
      string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName); 
      FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName); 
      Response.Redirect(Request.Url.AbsoluteUri); 
     } 
     protected void DownloadFile(object sender, EventArgs e) 
     { 
      string filePath = (sender as LinkButton).CommandArgument; 
      Response.ContentType = ContentType; 
      Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath)); 
      Response.WriteFile(filePath); 
      Response.End(); 
     } 
     protected void DeleteFile(object sender, EventArgs e) 
     { 
      string filePath = (sender as LinkButton).CommandArgument; 
      File.Delete(filePath); 
      Response.Redirect(Request.Url.AbsoluteUri); 
     } 
     protected void ViewFile(object sender, EventArgs e) 
     { 
      //string filePath = (sender as LinkButton).CommandArgument; 
      // File.ReadAllLines(filePath); 
      //GridView2.DataSource = File.ReadAllLines(filePath); 
      //GridView2.DataBind(); 
      //string[] content = File.ReadAllLines(filePath); 

      //GridView2.DataSource = content. 
      // OleDbConnection conn = new OleDbConnection(); 
      // conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Users/poonamj/Documents/Visual Studio 2008/Projects/SmartTool/SmartTool/fallout.accdb;User id=admin;Password="; 
      // conn.Open(); 

      string Access = Server.MapPath("App_Data/fallout.accdb"); 
      string Excel = Server.MapPath("~/Uploads/"); 
      string connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Excel + ";Extended Properties=Excel 8.0;Mode=12;"; 
      using (OleDbConnection conn = new OleDbConnection(connect)) 
      { 
       using (OleDbCommand cmd = new OleDbCommand()) 
       { 
        cmd.Connection = conn; 
        cmd.CommandText = "SELECT * INTO [MS Access;Database=" + Access + "].[New Table] FROM [Sheet1$]"; 
        conn.Open(); 
        cmd.ExecuteNonQuery(); 
        conn.Close(); 
       } 
      } 

     } 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/")); 
       List<ListItem> files = new List<ListItem>(); 
       foreach (string filePath in filePaths) 
       { 
        files.Add(new ListItem(Path.GetFileName(filePath), filePath)); 
       } 
       GridView1.DataSource = files; 
       GridView1.DataBind(); 
      } 

     } 




    } 
} 

回答

2

我想评论,但不能。

你检查了以下吗?每个人都可以找到可以帮助你的解决方案。

微软官方支持页面http://support.microsoft.com/kb/306269

此外,可尝试使用完整路径,包括为数据源的文件名。以下是有缺陷的:

string Excel = Server.MapPath("~/Uploads/"); 

想在Uploads/后附加Excel文件名。

+0

文件名一直不一样。它是动态的,所以我不能指定文件名:( – user1763769 2013-02-21 05:20:02

+0

)定义一个包含当前文件名的字符串变量,保持最新并在'uploads \'后面追加它的值,例如:'Server.MapPath(“〜/上传/“&strVariableContainingFileName)'。这是否有帮助? – user1750028 2013-02-21 05:24:49

+0

嘿其工作...字符串filePath =(发件人为LinkBut​​ton).CommandArgument;字符串Excel = Server.MapPath(Path.GetFileName(filePath)); – user1763769 2013-02-21 05:39:51

0

我已经使用过这样的工具来解决过去的问题:http://www.filehippo.com/download_unlocker/,更重要的是它是目录功能上的视图锁。我不知道,如果我正在推翻这个,但是'C:\ Users \ poonamj \ Documents \ Visual Studio 2008 \ Projects \ SmartTool \ SmartTool \ Uploads \'是一个目录,它似乎期待一个文件。

一个例子是'C:\ Users \ poonamj \ Documents \ Visual Studio 2008 \ Projects \ SmartTool \ SmartTool \ Uploads \ db.mdb'是文件的路径。

-1

您没有为Excel指定文件名并导致该异常。

string Excel = Server.MapPath("~/Uploads/MyFile.xls"); 
string connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Excel + ";Extended Properties=Excel 8.0;Mode=12;"; 

将文件名添加到Excel中,应解决此问题。

+0

文件名称始终不相同。它是动态的,所以我不能指定文件名:( – user1763769 2013-02-21 05:19:55

相关问题