创建SQL Server ODBC数据源的最简单,最可维护的方法是什么? [英] What is the simplest, most maintainable way to create a SQL Server ODBC Data Source?

查看:98
本文介绍了创建SQL Server ODBC数据源的最简单,最可维护的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种编程方式来创建SQL Server ODBC数据源.我可以通过直接访问注册表来做到这一点.最好通过可用的(SQL Server/Windows)API来完成此操作,以防止注册表项或更新的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.

可接受的答案注意::使用SQLConfigDataSource可从注册表项等的详细信息中提取代码,因此更加健壮.但是,我希望SQL Server可以将它包装在一个更高级别的函数中,该函数采用强类型属性(而不是定界字符串),并通过驱动程序公开它.

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.

推荐答案

SQLConfigDataSource()完成工作.

SQLConfigDataSource() does the job.

MSDN文章

以防这是一个VB6示例:

Just in case here is a VB6 example:

Const ODBC_ADD_DSN = 1 'user data source
Const ODBC_ADD_SYS_DSN = 4 'system data source

Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal
hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal
lpszAttributes As String) As Long

strDriver = "SQL Server"
strAttributes = "DSN=Sample" & Chr$(0) _
& "Database=Northwind" & Chr$(0) _
& "Description= Sample Data Source" & Chr$(0) _
& "Server=(local)" & Chr$(0) _
& "Trusted_Connection=No" & Chr$(0)

SQLConfigDataSource(0, ODBC_ADD_SYS_DSN, strDriver, strAttributes)

这篇关于创建SQL Server ODBC数据源的最简单,最可维护的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆