Creating Database at run time and registering it as DSN at run time
I am trying to create an ACCESS DB at run time and register is as a DSN in ODBC.ini using the following code:
BOOL fCreated;
fCreated = SQLConfigDataSource(NULL,ODBC_ADD_DSN,
"Microsoft Access Driver (*.mdb)",
"CREATE_DB=.\\ATest.mdb General\0" );
printf("Database created : %d\n",fCreated);
BOOL ReturnResult = SQLConfigDataSource (NULL, ODBC_CONFIG_DSN,
"Microsoft Access Driver (*.mdb)",
"DSN=TESTDSN_DSN\00DBQ=.\\ATest.mdb\ \
00FIL=MSAccess\00Description=TESTDSN_database\00UID=\00");
printf("Database registered : %d\n",ReturnResult);
The first state开发者_StackOverflow社区ment returns 1 showing that the DB is created but the second one returns 0 indicating that it could not be registered as a DataSourceName in ODBC.ini. Can anyone please tell me why is this happening because everything like path and name is correct.
It was already mentioned in the comments to the question, but I wanted to provide an answer with some more information:
Instead of creating a DSN by code on some machine, it's probably easier to link the tables DSN-less (programmatically) in the client that will access the database.
If the client is MS Access as well, you can do it with DoCmd.TransferDatabase
:
Dim SourceDB As String
Dim SourceTable As String
Dim LocalTable As String
'path to the database that contains the original table
SourceDB = "C:\Your_Database.mdb"
'name of the original table in the source database
SourceTable = "Original_Table"
'name of the linked table in the local database
LocalTable = "Linked_Table"
DoCmd.TransferDatabase acLink, "Microsoft Access", SourceDB, acTable, SourceTable, LocalTable
精彩评论