0

My question follows this one: Writing large number of records (bulk insert) to Access in .NET/C#

I've tried the method indicated with DAO. At first, it did not work, getting this error: Retrieving the COM class factory for component with CLSID {681EF637-F129-4AE9-94BB-618937E3F6B6} failed due to the following error: 80040154.) I've searched and found a solution with registry keys, it worked, but when I've tried on an other PC, the founction makes my application crach.

There is my code and where it fails:

using Microsoft.Office.Interop.Access.Dao;

public void ExportDataTableToAccess(System.Data.DataTable dtOutData)
{
    Microsoft.Office.Interop.Access.Application access = new Microsoft.Office.Interop.Access.Application();

    _DBEngine dbEngine = null;

    try
    {
        dbEngine = access.DBEngine; //Fails here

        //I have tried   DBEngine dbEngine = access.DBEngine
        //did not work either
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

    try
    {
        Boolean CheckFl = false;
        string DBPath = dbPath;

        Database db = dbEngine.OpenDatabase(dbPath);
        Recordset AccesssRecordset = db.OpenRecordset(dtOutData.TableName);
        Field[] AccesssFields = new Field[dtOutData.Columns.Count];

        //Loop on each row of dtOutData
        for (Int32 rowCounter = 0; rowCounter < dtOutData.Rows.Count; rowCounter++)
        {
            AccesssRecordset.AddNew();
            //Loop on column
            for (Int32 colCounter = 0; colCounter < dtOutData.Columns.Count; colCounter++)
            {
                // for the first time... setup the field name.
                if (!CheckFl)
                    AccesssFields[colCounter] = AccesssRecordset.Fields[dtOutData.Columns[colCounter].ColumnName];
                AccesssFields[colCounter].Value = dtOutData.Rows[rowCounter][colCounter];
            }

            AccesssRecordset.Update();
            CheckFl = true;
        }

        AccesssRecordset.Close();
        db.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
       System.Runtime.InteropServices.Marshal.ReleaseComObject(dbEngine);
    }
}

I've tried this solution too: How to repair COMException error 80040154?

It still works on my computer, but on no other one. And I don't know how to make all the manipulation on the registry on each computer to make it works (I don't want this solution either since it would be a very huge amount of time)

I know this is a 32-64 bit problem somewhere, but event the x86 build don't works on other computer... How should I resolve this problem ?

This is my second question on this site, please, if something is wrong with my question, tell me, and I will do everything possible to correct it :)

Community
  • 1
  • 1
Eradash
  • 352
  • 6
  • 14
  • Odds are good that the needed data access libraries aren't installed on the other computers. The MDAC libraries in the proper bit version (32or 64) need to be installed. Also note, they are rather finicky. – dbugger Jun 30 '16 at 13:55
  • ps. if it all possible try and move your database to SQLExpress or SQLite or something that is not Access. It will reduce deployment, development and debugging headaches. – dbugger Jun 30 '16 at 13:58
  • @dbugger My Access backend is on a NAS (I know, not the best solution, but works well until now). I'm not familiar with SQLExpress, SQLite or MySQL, but for what I've read, I will not be able to make the change (SQLite not working in networks, and for the other two I don't have any computer to run the DB engine and make it available to all users at all time) Since it is not for a big usage (4 or 5 users at the same time maximum), Access was good for me. The only thing Access is restraining me is the lack of easy fast bulk insert implementation, wich I try to do here... – Eradash Jun 30 '16 at 14:51

1 Answers1

1

Install the PIA Primary Interop Assemblies to work with the DAO Driver not being found.

juanvan
  • 587
  • 6
  • 18