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" (ByRef env As Long) As Long

解决方案 »

  1.   

    用代码建立DSN(SQL)
    Private 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
    Public Sub CreateDSN(sDSN As String)
    On Error Resume Next
    Dim nRet As Long
    Dim sDriver As String
    Dim sAttributes As String
    sDriver = "SQl server"
    sAttributes = "DSN=" & sDSN & Chr$(0)
    sAttributes = sAttributes & "Server=(local)" & Chr$(0)
    sAttributes = sAttributes & "Database=webstation" & Chr$(0)
    nRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_SYS_DSN, sDriver, sAttributes)
    End Sub
    Public Sub DeleteDSN(sDSN As String)
    On Error Resume Next
    Dim nRet As Long
    Dim sDriver As String
    Dim sAttributes As String
    sDriver = "SQl server"
    sAttributes = sAttributes & "DSN=" & sDSN & Chr$(0)
    nRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_SYS_DSN, sDriver, sAttributes)
    End Sub
    Private Sub Command1_Click()
    CreateDSN "Test"
    End Sub
    Private Sub Command2_Click()
    DeleteDSN "Test"
    End Sub
    ODBC DSN Connections
    Using an ODBC DSN (Data Source Name) is a two step process. 1) You must first create the DSN via the "ODBC Data Source Administrator" 
    program found in your computer's Control Panel (or Administrative Tools 
    menu in Windows 2000). Make sure to create a SYSTEM DSN (not a USER 
    DSN) when using ASP. You can also create the DSN via Visual Basic code.2) Then use the following connection string - with your own DSN 
    name of course.  ODBC - DSN oConn.Open "DSN=mySystemDSN;" & _ 
               "Uid=myUsername;" & _ 
               "Pwd=myPassword;" ODBC - File DSN oConn.Open "FILEDSN=c:\somepath\mydb.dsn;" & _ 
               "Uid=myUsername;" & _
               "Pwd=myPassword;"
    Visual Basic 编程资源大全里的一篇文章主题:如何动态新增、移除 ODBC DSN? 
    版本:VB6 / VB5 / VB4-32 一般我们建立 Client 端 DSN 都是在使用者的机器上进入【控制面板】【ODBC 数据源(32位)】去建立,但是如果我们开发的 APP 使用者很多时,这就有点累人了,所以我们可以將这个动作放在程序中!新增 DSN 的方法有二种:
    1、使用 DBEngine 控件的 RegisterDatabase 方法
    2、呼叫 SQLConfigDataSource API不管使用以上任何一种方法新增 DSN,一共会写入二个地方,一个是注册表,一个是 ODBC.INI。
    而刪除 DSN 的方法同上面的第二种方法,呼叫 SQLConfigDataSource API。以下之模块以 Oracle73 Ver 2.5 为例,在 Form 的声明区中加入以下声明及模块: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 PointerPrivate Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
    (ByVal hwndParent As Long, ByVal fRequest As Long, _
    ByVal lpszDriver As String, ByVal lpszAttributes As String) As LongPublic Sub CreateDSN(sDSN As String)
    Dim nRet As Long
    Dim sDriver As String
    Dim sAttributes As String
    sDriver = "Oracle73 Ver 2.5"
    sAttributes = "Server=Oracle8" & Chr$(0)
    sAttributes = sAttributes & "DESCRIPTION=" & sDSN & Chr$(0)
    'sAttributes = sAttributes & "DSN=" & sDSN & Chr$(0)
    sAttributes = sAttributes & "DATABASE=DBFinance" & Chr$(0)
    sAttributes = sAttributes & "Userid=Scott" & Chr$(0)
    'sAttributes = sAttributes & "PWD=myPassword" & Chr$(0)
    DBEngine.RegisterDatabase sDSN, sDriver, True, sAttributes '注一
    'nRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, sDriver, sAttributes) '注二
    End SubPublic Sub DeleteDSN(sDSN As String)
    Dim nRet As Long
    Dim sDriver As String
    Dim sAttributes As String
    sDriver = "Oracle73 Ver 2.5"
    sAttributes = sAttributes & "DSN=" & sDSN & Chr$(0)
    nRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, sDriver, sAttributes)
    End Sub
    '假设要产生的 DSN 为 Test,实际使用示例如下:Private Sub Command1_Click()
    CreateDSN "Test"
    End SubPrivate Sub Command2_Click()
    DeleteDSN "Test"
    End Sub
    '而写到系統的数据如下:
    1、ODBC.INI[ODBC 32 bit Data Sources]
    Test=Oracle73 Ver 2.5 (32 bit)[Test]
    Driver32=C:\ORAWIN95\ODBC250\sqo32_73.dll
    2、注册表机码:HKEY_CURRENT_USER\Software\ODBC\ODBC.INI\ODBC Data Sources
    名称:Test 数据:Oracle73 Ver 2.5机码:HKEY_CURRENT_USER\Software\ODBC\ODBC.INI\Test
    名称:Description 数据:Test
    名称:Driver 数据:C:\ORAWIN95\ODBC250\sqo32_73.dll
    名称:Server 数据:Oracle8
    名称:UserId 数据:Scott
    ※注一及注二可任选一种,只要將不使用的方法 Mark 起來即可!
    ※若您想使用其他之数据库,只要將以上模块稍作修改即可!