在运行时创建数据库并在运行时将其注册为 DSN

发布于 2024-10-01 18:36:42 字数 671 浏览 0 评论 0原文

我正在尝试在运行时创建 ACCESS DB,并使用以下代码在 ODBC.ini 中注册为 DSN:

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);

第一个语句返回 1,表明数据库已创建,但第二个语句返回 0,表明无法注册作为 ODBC.ini 中的 DataSourceName。谁能告诉我为什么会发生这种情况,因为路径和名称等所有内容都是正确的。

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 statement 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼睛不忧郁 2024-10-08 18:36:42

在问题的评论中已经提到了这一点,但我想提供一个包含更多信息的答案:

与其在某些机器上通过代码创建 DSN,不如在客户端中链接无 DSN(以编程方式)的表可能更容易这将访问数据库。

如果客户端也是 MS Access,则可以使用 DoCmd 来完成此操作。传输数据库

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文