Reputation: 31
How can I create an MS Access database using C# and ODBC?
Upvotes: 0
Views: 1429
Reputation: 82146
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.
Upvotes: 1