【问题标题】:The Microsoft Jet database engine cannot open the file. It is already opened exclusively by another user, or you need permission to view its data.Microsoft Jet 数据库引擎无法打开该文件。它已被其他用户独占打开,或者您需要权限才能查看其数据。
【发布时间】:2013-02-20 16:40:56
【问题描述】:

我检查了上面的注释,但它们没有帮助。我使用 VS2008 for ASP.Net 和 MS Access 2010 作为数据库。 我需要通过 ASP 网页将数据从 excel 上传到数据库。

但我收到如下错误:

“Microsoft Jet 数据库引擎无法打开文件'C:\Users\poonamj\Documents\Visual Studio 2008\Projects\SmartTool\SmartTool\Uploads\'。它已被其他用户以独占方式打开,或者您需要权限查看其数据。”

    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();
            }

        }




    }
}

【问题讨论】:

    标签: asp.net ms-access-2010


    【解决方案1】:

    我想发表评论,但不能。

    您是否检查了以下内容?每个人都列出了可以帮助您的可能解决方案。

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

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

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

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

    【讨论】:

    • 文件名一直不一样。它是动态的,所以我无法指定文件名:(
    • 定义一个包含当前文件名的字符串变量。保持最新并将其值附加在Uploads\ 之后。例如:Server.MapPath("~/Uploads/" &amp; strVariableContainingFileName)。这有帮助吗?
    • 嘿它的工作... string filePath = (sender as LinkBut​​ton).CommandArgument;字符串 Excel = Server.MapPath(Path.GetFileName(filePath));
    • 但现在收到不同的错误消息...无法更新。数据库或对象是只读的。 :(
    • 检查您正在使用的 Excel 文件的权限。右键单击该文件并打开 Properties。确保它没有在Attributes 下标记为Read-Only
    【解决方案2】:

    我过去使用过这样的工具来解决问题: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' 是文件的路径。

    【讨论】:

      【解决方案3】:

      您没有为 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 中添加一个文件名,应该可以解决这个问题。

      【讨论】:

      • 文件名一直不一样。它是动态的,所以我无法指定文件名:(
      猜你喜欢
      • 2011-11-08
      • 1970-01-01
      • 2012-01-04
      • 2023-03-14
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      • 2021-11-12
      相关资源
      最近更新 更多