创建 SQL Server ODBC 数据源的最简单、最可维护的方法是什么?
我需要一种编程方式来创建 SQL Server ODBC 数据源。 我可以通过直接访问注册表来做到这一点。 如果可以通过可用的 (SQL Server/Windows) API 来完成此操作,以防止注册表项或值因更新的 SQL Server 驱动程序而发生更改,那就更好了。
接受的答案注释:使用 SQLConfigDataSource 从注册表项等详细信息中抽象代码,因此更加可靠。 然而,我希望 SQL Server 能够使用更高级别的函数来包装它,该函数采用强类型属性(而不是分隔字符串)并通过驱动程序公开它。
I need a programmatic way of creating a SQL Server ODBC Data Source. I can do this by directly accessing the Registry. It would be better if this could be done via an available (SQL Server/Windows) API to protect against changes in the registry keys or values with updated SQL Server drivers.
Accepted Answer Note: Using SQLConfigDataSource abstracts the code from the details of Registry keys etc. so this is more robust. I was hoping, however, that SQL Server would have wrapped this with a higher level function which took strongly typed attributes (rather than a delimited string) and exposed it through the driver.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
SQLConfigDataSource() 完成这项工作。
MSDN 文章
以防万一,这里有一个 VB6 示例:
SQLConfigDataSource() does the job.
MSDN article
Just in case here is a VB6 example:
对于 VB.NET,可以通过以下方式完成:
导入“DllImport”:
声明 SQLConfigDataSource:
示例用法:
For VB.NET it can be done this way:
Import for 'DllImport':
Declaration of SQLConfigDataSource:
Example usage:
我将使用位于 system32 文件夹中的 odbcad32.exe。
这会将您的 odbc 数据源添加到正确的位置,该位置不会受到任何补丁的影响。
I'd use odbcad32.exe which is located in your system32 folder.
This will add your odbc data sources to the correcct location, which won't be effected by any patches.
要直接在注册表中执行此操作,您可以将字符串值添加到:
添加系统 DSN,或者:
添加用户 DSN。
值的名称是您要创建的数据源的名称,数据必须是“SQL Server”。
在注册表中与“ODBC 数据源”相同的级别上,使用要创建的数据源的名称创建一个键。
该键需要以下字符串值:
例如,从命令行使用 reg.exe 应用程序添加名为“ExampleDSN”的用户数据源:
To do this directly in the registry you can add a String Value to:
to add a System DSN, or:
to add a User DSN.
The Name of the Value is the name of the Data Source you want to create and the Data must be 'SQL Server'.
At the same level as 'ODBC Data Sources' in the Registry create a Key with the name of the Data Source you want to create.
This key needs the following String Values:
For example, using the reg.exe application from the command line to add a User Data Source called 'ExampleDSN':
使用 C# 的示例:(
详细的 SQL Server 参数参考位于 http://msdn.microsoft .com/en-us/library/aa177860.aspx)
Sample Using C#:
( Detailed SQL Server param reference at http://msdn.microsoft.com/en-us/library/aa177860.aspx )