我也遇到过相同的问题,你试一下吧 ’api函数申明 Private Const ODBC_ADD_SYS_DSN = 4 Private Declare Function SQLConfigDataSource Lib "odbccp32.dll" (ByVal hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As String) As LongPublic Function LoadDbSource2(strDriver, strAttributes As String) As Boolean LoadDbSource2 = SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, strDriver, strAttributes) End Functio 'sql server如下: Dim intRet As Long Dim strDriver As String Dim strAttributes As String strDriver = "SQL Server" strAttributes = "SERVER=myserver" & Chr$(0) strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0) strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr$(0) strAttributes = strAttributes & "DATABASE=mydb" & Chr$(0) strAttributes = strAttributes & "UID=sa" & Chr$(0) strAttributes = strAttributes & "PWD=" & Chr$(0) intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes) If intRet Then MsgBox "DSN 建立" Else MsgBox "失败" End If '生成access数据源 Dim StrAttributes As String StrAttributes = "DSN=AccessODBC2" & Chr(0) & "Desciption=动态加载ODBC示例" & Chr(0) StrAttributes = StrAttributes & "Dbq=" & App.Path & "\MyDb.mdb" & Chr(0) &"FIL=MS Access;" & Chr(0) StrAttributes = StrAttributes & "MaxBufferSize=2048" & Chr(0) & "PageTimeout=5" & Chr(0) Call LoadDbSource2("Microsoft Access Driver (*.mdb)", StrAttributes)
我的创建过程为什么总是失败? Private Const ODBC_ADD_SYS_DSN = 4 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 Private Sub Form_Load() Dim intRet As Long Dim strDriver As String Dim strAttributes As String strDriver = "SQL Server" strAttributes = "SERVER=myserver" strAttributes = strAttributes & "DESCRIPTION=Temp DSN" strAttributes = strAttributes & "DSN=DSN_TEMP" strAttributes = strAttributes & "DATABASE=mydb" intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes) If intRet Then MsgBox "DSN 建立" & intRet Else MsgBox "失败" & intRet End If End Sub
[HKEY_LOCAL_MACHINE\SOFTWARE\ODBC]
更具体的内容里可以自己看看注册表,不难!
’api函数申明
Private Const ODBC_ADD_SYS_DSN = 4
Private Declare Function SQLConfigDataSource Lib "odbccp32.dll" (ByVal hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As String) As LongPublic Function LoadDbSource2(strDriver, strAttributes As String) As Boolean
LoadDbSource2 = SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, strDriver, strAttributes)
End Functio
'sql server如下:
Dim intRet As Long
Dim strDriver As String
Dim strAttributes As String
strDriver = "SQL Server"
strAttributes = "SERVER=myserver" & Chr$(0)
strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)
strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr$(0)
strAttributes = strAttributes & "DATABASE=mydb" & Chr$(0)
strAttributes = strAttributes & "UID=sa" & Chr$(0)
strAttributes = strAttributes & "PWD=" & Chr$(0)
intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
If intRet Then
MsgBox "DSN 建立"
Else
MsgBox "失败"
End If
'生成access数据源
Dim StrAttributes As String
StrAttributes = "DSN=AccessODBC2" & Chr(0) & "Desciption=动态加载ODBC示例" & Chr(0)
StrAttributes = StrAttributes & "Dbq=" & App.Path & "\MyDb.mdb" & Chr(0) &"FIL=MS Access;" & Chr(0)
StrAttributes = StrAttributes & "MaxBufferSize=2048" & Chr(0) & "PageTimeout=5" & Chr(0)
Call LoadDbSource2("Microsoft Access Driver (*.mdb)", StrAttributes)
Private Const ODBC_ADD_SYS_DSN = 4
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
Private Sub Form_Load()
Dim intRet As Long
Dim strDriver As String
Dim strAttributes As String
strDriver = "SQL Server"
strAttributes = "SERVER=myserver"
strAttributes = strAttributes & "DESCRIPTION=Temp DSN"
strAttributes = strAttributes & "DSN=DSN_TEMP"
strAttributes = strAttributes & "DATABASE=mydb"
intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
If intRet Then
MsgBox "DSN 建立" & intRet
Else
MsgBox "失败" & intRet
End If
End Sub
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q171/1/46.asp&NoWebContent=1