5

I need to create an access (mdb) database without using the ADOX interop assembly.

How can this be done?

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
Sam Saffron
  • 128,308
  • 78
  • 326
  • 506

5 Answers5

11

Before I throw away this code, it might as well live on stackoverflow

Something along these lines seems to do the trick:

if (!File.Exists(DB_FILENAME))
{
    var cnnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DB_FILENAME;

    // Use a late bound COM object to create a new catalog. This is so we avoid an interop assembly. 
    var catType = Type.GetTypeFromProgID("ADOX.Catalog");
    object o = Activator.CreateInstance(catType);
    catType.InvokeMember("Create", BindingFlags.InvokeMethod, null, o, new object[] {cnnStr});

    OleDbConnection cnn = new OleDbConnection(cnnStr);
    cnn.Open();
    var cmd = cnn.CreateCommand();
    cmd.CommandText = "CREATE TABLE VideoPosition (filename TEXT , pos LONG)";
    cmd.ExecuteNonQuery();

}

This code illustrates that you can access the database using OleDbConnection once its created with the ADOX.Catalog COM component.

Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
  • I am getting Invocation error in this line: `catType.InvokeMember("Create", BindingFlags.InvokeMethod, null, o, new object[] {cnnStr});` – Rashad May 05 '14 at 08:45
1

I've done the same as Autsin, create an Access db then included it into my project as a managed resource. Once there, it is included in the compiled code and you can copy it to hard disk as many times as you want. Empty databases are relatively small too, so there isn't much overhead.

The added bonus is the ability to set up the database if you know how it will be used or what tables will be added every time, you can reduce the amount of coding and slow database queries.

Fry
  • 4,106
  • 9
  • 38
  • 51
0

You don't need Jet(major headache) installed, if you use this connection string in .net 3.5

Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\myFolder\myAccess2007file.accdb;Persist
Security Info=False;

This should work on access 2007 and below

Aaron Fischer
  • 20,853
  • 18
  • 75
  • 116
  • Offtopic, do you know if this works on X64? Is this new to 3.5? – Sam Saffron Oct 01 '08 at 01:42
  • This is new to the 3.5 framework. I am not sure if its x64 compatible or not. – Aaron Fischer Oct 01 '08 at 02:25
  • Jet 4 is installed on all versions of Windows since Windows 2000, so why would you be worrying about bypassing Jet? The ACE is not necessarily going to be installed, so I don't see why you're recommending ACE. Is it distributed with the .NET 3.5 runtime? – David-W-Fenton Jan 28 '09 at 20:11
  • Agreed, Jet 4.0 is more likely to already be installed. ACE available as a free download from Micsrosoft (http://www.microsoft.com/downloads/details.aspx?FamilyId=7554F536-8C28-4598-9B72-EF94E038C891&displaylang=en). – onedaywhen Jan 29 '09 at 08:45
  • I don't believe Jet is installed by default on Windows XP and higher. It has not been a part of MDAC since windows 2000. It only gets installed with older versions of MS Access. ACE is supposed to be part of .NET 3.5 – Aaron Fischer Jan 29 '09 at 15:17
  • Jet is part of the Windows OS, starting with Windows 2000. That's why it's no longer in the MDAC, because all versions of Windows from 2000 on already include it. – David-W-Fenton Oct 27 '09 at 20:41
  • Aaron, how can you create a new Access database with this method? Using this connection string just allows you to connect to an existing one (no?) – Jess Chadwick Dec 26 '09 at 06:49
  • If memory serves you just issue a create table command and the provider will create the file for you. – Aaron Fischer Dec 26 '09 at 16:50
0

Interesting question -- I've never thought to create one on the fly like this. I've always included my baseline database as a resource in the project and made a copy when I needed a new one.

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
0

ACE is not in any framework (yet. ie not in 1, 2, 3.5, 4, 4.5)

It's also not part of Windows Update.

JET4 is in Framework2 and above.

If you're working with Access/MDB files etc then do not assume ACE is present.

gustavohenke
  • 40,997
  • 14
  • 121
  • 129
Ian
  • 1