1

I recently tried to use access database with C# code inside a little Revit plugin but it crash when I use OleDbConnection.Open()

Here is my snippets:

    CPFMainModelView mainModelView;
    static readonly string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\ThomasLECUPPRE(Letit\source\LIB_MainDB.accdb";

    public UserDBManager(CPFMainModelView cmmw)
    {
        mainModelView = cmmw;
        try
        {
            RetrieveprojectList();
        }
        catch (Exception ex)
        {
            mainModelView.Texte = $"{ex.Message}\n\n{ex.StackTrace}\n\n{ex.InnerException}\n\n{ex.Data}";
        }

    }

    public void RetrieveprojectList()
    {
        using (OleDbConnection con = new OleDbConnection(connectionString))
        {
            con.Open();
            OleDbCommand command = new OleDbCommand("SELECT Ref FROM FolderCategory", con);
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                mainModelView.Texte += $"\n{reader["Ref"]}";
            }
        }
    }

Here is a view of my tiny db in access

What did I miss ?

I have already see these thread : C# - OleDbConnection.Open() causing a crash Simple C# connection to .accdb file

Before use "Provider=Microsoft.ACE.OLEDB.12.0" and build the solution for X86 only, I was using "Provider=Microsoft.Jet.OLEDB.4.0" and build solutino for Any CPU but this thread (in french sorry) tell to it another way.

Thank you for help.

Thomas_Lcp
  • 19
  • 5
  • Which exception do you get and where? – Klaus Gütter Nov 04 '21 at 14:27
  • App just crash, no exception, nothing, that is the issue :/ With "Provider=Microsoft.Jet.OLEDB.4.0" and build solutino for Any CPU I simply get "Provider "blablabla" is not registered on the local computer" (traducted from "Le fournisseur 'Provider=Microsoft.Jet.OLEDB.4.0' n'est pas inscrit sur l'ordinateur local".) – Thomas_Lcp Nov 04 '21 at 14:30
  • @KlausGütter I just tried it with "Provider= ....12.0" and I get the same exception as Provider=....4.0 Why my plugin doesn't show this exception and show it for 4.0 provider ?! – Thomas_Lcp Nov 04 '21 at 14:49
  • Hava a look in the Windows Event log. An App crash usually leaves some useful information there – Klaus Gütter Nov 04 '21 at 15:02

1 Answers1

0

Install the missing dependancy

SO post

On modern Windows this driver isn't available by default anymore, but you can download as Microsoft Access Database Engine 2010 Redistributable on the MS site. If your app is 32 bits be sure to download and install the 32 bits variant because to my knowledge the 32 and 64 bit variant cannot coexist.

Depending on how your app locates its db driver, that might be all that's needed. However, if you use an UDL file there's one extra step - you need to edit that file. Unfortunately, on a 64bits machine the wizard used to edit UDL files is 64 bits by default, it won't see the JET driver and just slap whatever driver it finds first in the UDL file. There are 2 ways to solve this issue:

  1. start the 32 bits UDL wizard like this: C:\Windows\syswow64\rundll32.exe "C:\Program Files (x86)\Common Files\System\Ole DB\oledb32.dll",OpenDSLFile C:\path\to\your.udl. Note that I could use this technique on a Win7 64 Pro, but it didn't work on a Server 2008R2 (could be my mistake, just mentioning)
  2. open the UDL file in Notepad or another text editor, it should more or less have this format:

[oledb] ; Everything after this line is an OLE DB initstring Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Path\To\The\database.mdb;Persist Security Info=False

That should allow your app to start correctly.

Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28
  • 1
    Doesn't work with "Provider=Microsoft.ACE.OLEDB.12.0" but do for "Provider=Microsoft.Jet.OLEDB.4.0" now I get a unrecognized System.Data.OleDb.OleDbException : 'Format de base de données ('C:\Users\ThomasLECUPPRE(Letit\source\LIB_MainDB.accdb') non reconnu.' But it's another story, thank you for these link ! – Thomas_Lcp Nov 04 '21 at 15:02
  • I used List inside Acces and that is why I get this exception. I reorganized my database I know, no problem. – Thomas_Lcp Nov 05 '21 at 08:18
  • I don't specify one thing, I tested this inside wpf app but when I use it in Revivt plugin, I get the same issue. – Thomas_Lcp Nov 05 '21 at 08:41