简体   繁体   中英

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." at 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".

  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://www.vbforums.com/showthread.php?398478-RESOLVED-Webform-error-quot-It-is-already-opened-exclusively-by-another-user-or-you-need-(-)-quot

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. Don`t forget! Finally you should delete the copy version. Below is your new InsertExcelRecords:

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

Enjoy...

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