利用ODBC api添加dns方法.
'Constant Declaration
Private Const ODBC_ADD_DSN = 1 ' Add data source
Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
Private Const vbAPINull As Long = 0& ' NULL Pointer
Private Declare Function SQLAllocEnv Lib "odbc32.dll" (phenv&) As Integer
'Function Declare
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
Option ExplicitPrivate Sub Command1_Click()
Dim intRet As Long
Dim strDriver As String
Dim strAttributes As String
strDriver = "Microsoft Access Driver (*.mdb)"
'Set the attributes delimited by null.
'See driver documentation for a complete list of attributes.
strAttributes = "DSN=DSN_TEMP" & Chr$(0)
'To show dialog, use Form1.Hwnd instead of vbAPINull.
intRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN,
strDriver, strAttributes)
If intRet Then
MsgBox "DSN Deleted"
Else
MsgBox "Delete Failed"
End IfEnd SubPrivate Sub Command2_Click()
Dim intRet As Long
Dim strDriver As String
Dim strAttributes As String
Dim rc As Integer 'ODBC 函 数 的 返 回 码
Dim henv As Long 'ODBC 环 境 句 柄
rc = SQLAllocEnv(henv) ' 获 取ODBC 环 境 句 柄
strDriver = "Microsoft Access Driver (*.mdb)"
strAttributes = "SERVER=SomeServer" & Chr$(0)
strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)
strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr$(0)
strAttributes = strAttributes & "DBQ=d:\pdzl.mdb" & Chr$(0)
strAttributes = strAttributes & "UID=admin" & Chr$(0)
strAttributes = strAttributes & "PWD=" & Chr$(0)
'To show dialog, use Form1.Hwnd instead of vbAPINull.
intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, _
strDriver, strAttributes)
If intRet Then
MsgBox "DSN Created"
Else
MsgBox "Create Failed"
End If
End Sub
'Constant Declaration
Private Const ODBC_ADD_DSN = 1 ' Add data source
Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
Private Const vbAPINull As Long = 0& ' NULL Pointer
Private Declare Function SQLAllocEnv Lib "odbc32.dll" (phenv&) As Integer
'Function Declare
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
Option ExplicitPrivate Sub Command1_Click()
Dim intRet As Long
Dim strDriver As String
Dim strAttributes As String
strDriver = "Microsoft Access Driver (*.mdb)"
'Set the attributes delimited by null.
'See driver documentation for a complete list of attributes.
strAttributes = "DSN=DSN_TEMP" & Chr$(0)
'To show dialog, use Form1.Hwnd instead of vbAPINull.
intRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN,
strDriver, strAttributes)
If intRet Then
MsgBox "DSN Deleted"
Else
MsgBox "Delete Failed"
End IfEnd SubPrivate Sub Command2_Click()
Dim intRet As Long
Dim strDriver As String
Dim strAttributes As String
Dim rc As Integer 'ODBC 函 数 的 返 回 码
Dim henv As Long 'ODBC 环 境 句 柄
rc = SQLAllocEnv(henv) ' 获 取ODBC 环 境 句 柄
strDriver = "Microsoft Access Driver (*.mdb)"
strAttributes = "SERVER=SomeServer" & Chr$(0)
strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)
strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr$(0)
strAttributes = strAttributes & "DBQ=d:\pdzl.mdb" & Chr$(0)
strAttributes = strAttributes & "UID=admin" & Chr$(0)
strAttributes = strAttributes & "PWD=" & Chr$(0)
'To show dialog, use Form1.Hwnd instead of vbAPINull.
intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, _
strDriver, strAttributes)
If intRet Then
MsgBox "DSN Created"
Else
MsgBox "Create Failed"
End If
End Sub
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货