Private Const SQL_SUCCESS As Long = 0
Private Const SQL_ERROR As Long = -1
Private Const SQL_FETCH_NEXT As Long = 1
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 Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv As Long, ByVal fDirection As Integer, ByVal szDSN As String, ByVal cbDSNMax As Integer, pcbDSN As Integer, ByVal szDescription As String, ByVal cbDescriptionMax As Integer, pcbDescription As Integer) As Integer
Private Declare Function SQLAllocEnv Lib "ODBC32.DLL" (env As Long) As Integer
'创建数据源 成功返回TRUE,失败返回FALSE
Public Function fun_CreateDSN(ByVal DSNname As String, ByVal ODBCdriver As String, ByVal DSNtype As e_DSNtype, _
ByVal SVRname As String, ByVal DBname As String, ByVal User As String, _
ByVal pwd As String, ByVal DSNdesc As String) As Boolean
' DSNname:数据源名
' ODBCdriver:数据源驱动
' DSNtype:数据源类型(系统、用户)
' SVRname:服务器名称
' DBname:数据库名
' User:用户名
' PWD:密码
' DSNdesc:数据源描述
On Error Resume Next
Dim nRet As Long
Dim sAttributes As String
If DSNname <> "" Then sAttributes = "DSN=" & DSNname & Chr$(0)
If DSNdesc <> "" Then sAttributes = sAttributes & "DESCRIPTION=" & DSNdesc & Chr$(0)
If SVRname <> "" Then sAttributes = sAttributes & "SERVER=" & SVRname & Chr$(0)
If User <> "" Then sAttributes = sAttributes & "UID=" & User & Chr$(0)
If pwd <> "" Then sAttributes = sAttributes & "PWD=" & pwd & Chr$(0)
If InStr(1, LCase$(ODBCdriver), "access") > 0 Then
If DBname <> "" Then sAttributes = sAttributes & "DBQ=" & DBname & Chr$(0)
ElseIf InStr(1, LCase$(ODBCdriver), "sql server") > 0 Then
sAttributes = "DSN=" & DSNname & Chr$(0) & "Server=" & SVRname & Chr$(0) & _
"UseProcForPrepare=Yes" & Chr$(0)
Else
If DBname <> "" Then sAttributes = sAttributes & "DATABASE=" & DBname & Chr$(0)
End If
If DSNtype = eSysDSN Then
nRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_SYS_DSN, ODBCdriver, sAttributes)
ElseIf DSNtype = eUserDSN Then
nRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, ODBCdriver, sAttributes)
Else
fun_CreateDSN = False
Exit Function
End If
If nRet = 0 Then
fun_CreateDSN = False
Else
fun_CreateDSN = True
End If
End Function
'删除数据源,成功返回TRUE,失败返回FALSE
Public Function fun_DeleteDSN(ByVal DSNname As String, ByVal ODBCdriver As String, ByVal DSNtype As e_DSNtype) As Boolean
' DSNname:数据源名
' ODBCdriver:数据源驱动
' DSNtype:数据源类型(系统、用户)
On Error Resume Next
Dim nRet As Long
Dim sAttributes As String
sAttributes = sAttributes & "DSN=" & DSNname & Chr$(0)
If DSNtype = eSysDSN Then
nRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_SYS_DSN, ODBCdriver, sAttributes)
ElseIf DSNtype = eUserDSN Then
nRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, ODBCdriver, sAttributes)
Else
fun_DeleteDSN = False
Exit Function
End If
If nRet = 0 Then
fun_DeleteDSN = False
Else
fun_DeleteDSN = True
End If
End Function
例如:
dim bRet as boolean
bRet = fun_CreateDSN("Test", "Microsoft Access Driver (*.mdb)", eSysDSN, "", "C:\Bible.mdb", "", "", "Just a Test")
If bRet Then
MsgBox "Create DSN Success!", vbInformation
Else
MsgBox "Create DSN Fault!", vbExclamation
End If