简体   繁体   English

Microsoft Office Access 数据库引擎无法打开或写入文件

[英]The Microsoft Office Access database engine cannot open or write to the file

I am getting this below exception我收到以下异常

"The Microsoft Office Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data." “Microsoft Office Access 数据库引擎无法打开或写入文件 ''。它已被其他用户以独占方式打开,或者您需要查看和写入其数据的权限。” at Econ.Open();在 Econ.Open();

public partial class LBMIS1New : System.Web.UI.Page
{
    OleDbConnection Econ;
    SqlConnection con;

    string constr, Query, sqlconn;
    protected void Page_Load(object sender, EventArgs e)
    {


    }

    private void ExcelConn(string FilePath)
    {

        constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", FilePath);
        Econ = new OleDbConnection(constr);

    }
    private void connection()
    {
        sqlconn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        con = new SqlConnection(sqlconn);

    }


    private void InsertExcelRecords(string FilePath)
    {
        ExcelConn(FilePath);

        Query = string.Format("Select [Name],[City],[Address],[Designation] FROM [{0}]", "Sheet1$");
        OleDbCommand Ecom = new OleDbCommand(Query, Econ);
        Econ.Open();

        DataSet ds = new DataSet();
        OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);
        Econ.Close();
        oda.Fill(ds);
        DataTable Exceldt = ds.Tables[0];
        connection();
        //creating object of SqlBulkCopy    
        SqlBulkCopy objbulk = new SqlBulkCopy(con);
        //assigning Destination table name    
        objbulk.DestinationTableName = "Employee";
        //Mapping Table column    
        objbulk.ColumnMappings.Add("Name", "Name");
        objbulk.ColumnMappings.Add("City", "City");
        objbulk.ColumnMappings.Add("Address", "Address");
        objbulk.ColumnMappings.Add("Designation", "Designation");
        //inserting Datatable Records to DataBase    
        con.Open();
        objbulk.WriteToServer(Exceldt);
        con.Close();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
        InsertExcelRecords(CurrentFilePath);
    }
}

This error can be caused by:此错误可能由以下原因引起:

  1. The file is still open on the machine or if it is on the network then someone still has it open.该文件仍然在机器上打开,或者如果它在网络上,那么有人仍然打开它。

  2. If the file is on a network, then this has caused this error due to security settings and the advice is to move the file locally onto the server.如果文件在网络上,则由于安全设置而导致此错误,建议将文件本地移动到服务器上。

  3. If the file is local then there are still permission issues that ares till present如果文件是本地文件,那么直到现在仍然存在权限问题

Solution解决方案

If you have already ensured that the file is not open anywhere else and you have copied it locally to the server please see the below steps:如果您已经确保该文件未在其他任何地方打开,并且您已将其本地复制到服务器,请参阅以下步骤:

  1. Ensure the file is saved locally on the server and is in a folder instead of being in the root folder.确保文件本地保存在服务器上,并且位于文件夹中,而不是位于根文件夹中。

  2. Once the file is in a folder, go to the privileges of that folder and make sure the following users have full access of read and write: "network service" and "asp".文件位于文件夹中后,转到该文件夹​​的权限并确保以下用户具有完全的读写权限:“网络服务”和“asp”。

  3. After granting these users access onto the folder and you still see the error, grant the user "Everyone" and test if this resolves the error.在授予这些用户访问文件夹的权限后,您仍然看到错误,授予用户“所有人”并测试这是否解决了错误。

For further information, you could refer to:有关更多信息,您可以参考:

http://community.landesk.com/support/docs/DOC-24626 http://community.landesk.com/support/docs/DOC-24626

http://www.vbforums.com/showthread.php?398478-RESOLVED-Webform-error-quot-It-is-already-opened-exclusively-by-another-user-or-you-need-(-)-quot http://www.vbforums.com/showthread.php?398478-RESOLVED-Webform-error-quot-It-is-already-opened-exclusively-by-another-user-or-you-need-(-)-报价

Regards问候

您可以使用文件的副本...

You can solve this problem by an innovative solution.您可以通过创新的解决方案来解决这个问题。 Work with a duplicate version of your file.使用文件的重复版本。 Such as changing your FilePath to the copy version of that.例如将您的 FilePath 更改为该副本的副本版本。 Don`t forget!不要忘记! Finally you should delete the copy version.最后,您应该删除复制版本。 Below is your new InsertExcelRecords:以下是您的新 InsertExcelRecords:

   private void NewInsertExcelRecords(string FilePath)
    {
        try{
                File.Copy(FilePath,FilePath += "tmp" + Path.GetExtension(FilePath));
                InsertExcelRecords(FilePath);
        }
        finally{File.Delete(FilePath);}
    }

Enjoy...享受...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft Office Access 数据库引擎无法打开或写入文件 '' - The Microsoft Office Access database engine cannot open or write to the file '' mdb文件oledbconnection引发“ Microsoft Access数据库引擎无法打开或写入文件…” - mdb file oledbconnection throws “The Microsoft Access database engine cannot open or write to the file …” Microsoft Access 数据库引擎无法打开或写入 windows 服务 c# 中的文件 - The Microsoft Access database engine cannot open or write to the file in windows service c# 打开与MS Access 2007文件的连接时出错:无法打开MS Office Access数据库引擎工作组信息文件 - Error while opening a connection to MS Access 2007 file: Cannot open the MS Office Access database engine workgroup information file 尝试查询Excel文件时出现问题…“ Microsoft Office Access数据库引擎找不到对象'Sheet1 $'。” - Issue attempting to query an Excel file… “The Microsoft Office Access database engine could not find the object 'Sheet1$'..” Microsoft Office Excel 无法访问 IIS 上的文件 - Microsoft Office Excel cannot access the file on IIS Microsoft Office 12.0 Access 数据库引擎 OLE DB 提供程序 - Microsoft Office 12.0 Access database engine OLE DB Provider Microsoft jet 数据库引擎无法打开文件。''。 已被其他用户独占打开,或者您需要权限 - Microsoft jet database engine cannot open the file.''. It is already opened exclusively by another user, or you need permission Microsoft Office Access数据库引擎找不到对象“ PPR_Status_Detailed” - The Microsoft Office Access database engine could not find the object 'PPR_Status_Detailed' Microsoft Jet数据库引擎无法打开文件“。它已经由另一个用户独占打开,或者您需要获得查看其数据的权限” - 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"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM