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#If Win32 ThenPrivate Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Long
#Else
Private Declare Function SQLConfigDataSource Lib "ODBCINST.DLL" (ByVal hwndParent As Integer, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Integer
#End IfPublic Function ODBCConfig() #If Win32 Then
Dim intRet As Long
#Else
Dim intRet As Integer
#End If
Dim strDriver As String
Dim strAttributes As String
strDriver = "SQL Server"
strAttributes = "Server=" & ServerName & Chr$(0)
strAttributes = strAttributes & "DESCRIPTION=说明了 " & Chr$(0)
strAttributes = strAttributes & "LastUser=sa" & Chr$(0)
strAttributes = strAttributes & "PWD=" & Chr$(0)
strAttributes = strAttributes & "DSN=" & strODBC & Chr$(0)
strAttributes = strAttributes & "DATABASE=" & DBName & Chr$(0)
intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
If intRet Then
'MsgBox "DSN Created"
Else
MsgBox "系统创建ODBC失败!", vbInformation, "提示"
End If
End Function
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#If Win32 ThenPrivate Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Long
#Else
Private Declare Function SQLConfigDataSource Lib "ODBCINST.DLL" (ByVal hwndParent As Integer, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Integer
#End IfPublic Function ODBCConfig() #If Win32 Then
Dim intRet As Long
#Else
Dim intRet As Integer
#End If
Dim strDriver As String
Dim strAttributes As String
strDriver = "SQL Server"
strAttributes = "Server=" & ServerName & Chr$(0)
strAttributes = strAttributes & "DESCRIPTION=说明了 " & Chr$(0)
strAttributes = strAttributes & "LastUser=sa" & Chr$(0)
strAttributes = strAttributes & "PWD=" & Chr$(0)
strAttributes = strAttributes & "DSN=" & strODBC & Chr$(0)
strAttributes = strAttributes & "DATABASE=" & DBName & Chr$(0)
intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
If intRet Then
'MsgBox "DSN Created"
Else
MsgBox "系统创建ODBC失败!", vbInformation, "提示"
End If
End Function
'In Form1
Option ExplicitPrivate Sub Command1_Click()
If CreateDSN(Text1.Text) Then MsgBox "The special DSN has been created successful!", vbInformation
End SubPrivate Sub Command2_Click()
If DeleteDSN(Text1.Text) Then MsgBox "The special DSN has been delete successful!", vbInformation
End SubPrivate Sub Form_Load()
Text1.Text = "Test"
Command1.Caption = "Create DSN"
Command2.Caption = "Delete DSN"
End Sub
'In Module1
Option ExplicitPrivate Declare Function SQLConfigDataSource Lib "odbccp32.dll" (ByVal hwndParent As Long, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As LongPrivate Const ODBC_ADD_DSN As Long = 1
Private Const ODBC_CONFIG_DSN As Long = 2
Private Const ODBC_REMOVE_DSN As Long = 3Public Function CreateDSN(ByVal sDSN As String) As Boolean
Dim sDriver As String
Dim sAttr As String
sDriver = "SQL Server"
'Which SQL Server do you want to connect to?
sAttr = "SERVER=HSSQLTest" & Chr$(0)
'DSN Name
sAttr = sAttr & "DSN=" & sDSN & Chr$(0)
'Change the default database to "ICPDB"
sAttr = sAttr & "DATABASE=ICPDB" & Chr$(0)
'With Windows NT authentication using the network login ID
sAttr = sAttr & "Trusted_Connection=Yes" & Chr$(0)
'Preform translation for character data
sAttr = sAttr & "AutoTranslate=No" & Chr$(0)
'Create
CreateDSN = SQLConfigDataSource(0&, ODBC_ADD_DSN, sDriver, sAttr)
End FunctionPublic Function DeleteDSN(ByVal sDSN As String) As Boolean
Dim sDriver As String
Dim sAttr As String
sDriver = "SQL Server"
sAttr = sAttr & "DSN=" & sDSN & Chr$(0)
DeleteDSN = SQLConfigDataSource(0&, ODBC_REMOVE_DSN, sDriver, sAttr)
End Function