0

How can I create an MS Access database using C# and ODBC?

1
  • 2
    I know this is probably not teh way to go, but have you considered embedding blank Access 2000, 2003, 2007 databases into your assembly, and write them to disk as required? I have used this technique successfully! Commented Aug 9, 2012 at 12:01

1 Answer 1

1

Judging from this: How do I specify the ODBC Access Driver Format when creating the database

I'd say like this:

enum RequestFlags : int
{
    ODBC_ADD_DSN = 1,
    ODBC_CONFIG_DSN = 2,
    ODBC_REMOVE_DSN = 3,
    ODBC_ADD_SYS_DSN = 4,
    ODBC_CONFIG_SYS_DSN = 5,
    ODBC_REMOVE_SYS_DSN = 6,
    ODBC_REMOVE_DEFAULT_DSN = 7
}


[DllImport("ODBCCP32.DLL",CharSet=CharSet.Unicode, SetLastError=true)]
static extern bool SQLConfigDataSourceW(UInt32 hwndParent , RequestFlags fRequest, string lpszDriver, string lpszAttributes);

Having this in your main method:

string strDriverName = "Microsoft Access Driver (*.mdb, *.accdb)";
string strAttr = "CREATE_DBV12=c:\access2007.accdb";
SQLConfigDataSource(0, RequestFlags.ODBC_ADD_DSN, strDriverName, strAttr );

Warning: Untested

Note: I copied the pInvoke info from pinvoke.net.
UInt32 hwndParent seems wrong, that should probably be UIntPtr.
This is especially important on Win64, which I think you need when I look at your tags.
If so, use UIntPtr.Zero instead of 0. But try first.

Also note that I wouldn't use an access database for new projects if they are intended to be large. If you need a standalone database, use Firebird embedded instead. That way you can save yourself a lot of trouble later.

Also note that newer 64-bit versions of windows ship without Access-driver preinstalled AFAIK.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.