1:在工程中引用:ODBC Driver & Data Source Name Functions
然后:
Dim sDsn As New Dsn
sDsn.CreateDSN DSNname, ODBCDriver, SVRname, DBname, USER, PWD, DSNdesc, Silent, ODBCAttr例子:
Dim sDsn As New Dsn
Dim bRet As Boolean
bRet = sDsn.CreateDSN("david", "Microsoft ODBC for Oracle", "LocalGps", "", "userid", "password", "just a test", True, "")
Debug.Print bRet
Set sDsn = Nothing2:apiPrivate Sub Command1_Click()
On Error Resume Next
Dim nRet As Long
Dim sDSN As String
Dim sDriver As String
Dim sAttributes As String
sDSN = "Test"
sDriver = "Microsoft ODBC for Oracle"
sAttributes = "DSN=" & sDSN & Chr$(0)
sAttributes = sAttributes & "DESCRIPTION=Just a test!" & Chr$(0)
sAttributes = sAttributes & "SERVER=server_name" & Chr$(0)
sAttributes = sAttributes & "UID=userID" & Chr$(0)
nRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_SYS_DSN, sDriver, sAttributes) 'system
'nRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, sDriver, sAttributes) 'user
'If nRet = 0 Then MsgBox "Error"
Debug.Print "Create:" & nRet
End Sub
然后:
Dim sDsn As New Dsn
sDsn.CreateDSN DSNname, ODBCDriver, SVRname, DBname, USER, PWD, DSNdesc, Silent, ODBCAttr例子:
Dim sDsn As New Dsn
Dim bRet As Boolean
bRet = sDsn.CreateDSN("david", "Microsoft ODBC for Oracle", "LocalGps", "", "userid", "password", "just a test", True, "")
Debug.Print bRet
Set sDsn = Nothing2:apiPrivate Sub Command1_Click()
On Error Resume Next
Dim nRet As Long
Dim sDSN As String
Dim sDriver As String
Dim sAttributes As String
sDSN = "Test"
sDriver = "Microsoft ODBC for Oracle"
sAttributes = "DSN=" & sDSN & Chr$(0)
sAttributes = sAttributes & "DESCRIPTION=Just a test!" & Chr$(0)
sAttributes = sAttributes & "SERVER=server_name" & Chr$(0)
sAttributes = sAttributes & "UID=userID" & Chr$(0)
nRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_SYS_DSN, sDriver, sAttributes) 'system
'nRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, sDriver, sAttributes) 'user
'If nRet = 0 Then MsgBox "Error"
Debug.Print "Create:" & nRet
End Sub
首先谢谢你的指教,我试你的例子的时候遇到这样几个问题,能不能在百忙之中帮我
解答一下,谢谢
sDSN.CreateDS("HHH", "SQL Server", "sw_server", "hos", "sa", "123", "text", True, "")
我的数据库是 sql server 下的 hos 服务器是 sw_server 我想建一个 HHH
用户名 sa 密码 123 这样写法为什么不对?哪里有问题?里面的参数 silent 到底有什么作用为什么我把他设成 false 函数能返回 真值但是
好像 dsn 里面没有东西请帮帮我,谢谢
eUserDSN = 0 '系统数据源
eSysDSN '用户数据源
End EnumPrivate Const ODBC_ADD_DSN = 1
Private Const ODBC_CONFIG_DSN = 2
Private Const ODBC_REMOVE_DSN = 3
Private Const ODBC_ADD_SYS_DSN = 4
Private Const ODBC_REMOVE_SYS_DSN = 6
Private Const vbAPINull As Long = 0&
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'创建数据源 成功返回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
http://www.csdn.net/expert/topic/878/878972.xml?temp=.8024561