如何通过编程来在ODBC数据源中添加一个系统DSN并配置它。

解决方案 »

  1.   

    odbc在注册表中的位置如下,然后里用注册表的api函数向里面写键值就可以了!
    [HKEY_LOCAL_MACHINE\SOFTWARE\ODBC]
    更具体的内容里可以自己看看注册表,不难!
      

  2.   

    我也遇到过相同的问题,你试一下吧
    ’api函数申明
    Private Const ODBC_ADD_SYS_DSN = 4
    Private Declare Function SQLConfigDataSource Lib "odbccp32.dll" (ByVal hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As String) As LongPublic Function LoadDbSource2(strDriver, strAttributes As String) As Boolean
    LoadDbSource2 = SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, strDriver, strAttributes)
    End Functio
    'sql server如下:
    Dim intRet As Long
    Dim strDriver As String
    Dim strAttributes As String
    strDriver = "SQL Server"
    strAttributes = "SERVER=myserver" & Chr$(0)
    strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)
    strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr$(0)
    strAttributes = strAttributes & "DATABASE=mydb" & Chr$(0)
    strAttributes = strAttributes & "UID=sa" & Chr$(0)
    strAttributes = strAttributes & "PWD=" & Chr$(0)
    intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
    If intRet Then
    MsgBox "DSN 建立"
    Else
    MsgBox "失败"
    End If
    '生成access数据源
    Dim StrAttributes As String
    StrAttributes = "DSN=AccessODBC2" & Chr(0) & "Desciption=动态加载ODBC示例" & Chr(0)
    StrAttributes = StrAttributes & "Dbq=" & App.Path & "\MyDb.mdb" & Chr(0) &"FIL=MS Access;" & Chr(0)
    StrAttributes = StrAttributes & "MaxBufferSize=2048" & Chr(0) & "PageTimeout=5" & Chr(0)
    Call LoadDbSource2("Microsoft Access Driver (*.mdb)", StrAttributes)
      

  3.   

    我的创建过程为什么总是失败?
    Private Const ODBC_ADD_SYS_DSN = 4
    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 Sub Form_Load()
    Dim intRet As Long
    Dim strDriver As String
    Dim strAttributes As String
    strDriver = "SQL Server"
    strAttributes = "SERVER=myserver"
    strAttributes = strAttributes & "DESCRIPTION=Temp DSN"
    strAttributes = strAttributes & "DSN=DSN_TEMP"
    strAttributes = strAttributes & "DATABASE=mydb"
    intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
    If intRet Then
    MsgBox "DSN 建立" & intRet
    Else
    MsgBox "失败" & intRet
    End If
    End Sub
      

  4.   

    在程序中建立DSN数据源:
    http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q171/1/46.asp&NoWebContent=1
      

  5.   

    编程建立的时候只要不加上用户名和密码那几句代码就会成功,而且是用户DSN,不是系统DSN。连微软的例子里面都没有加用户名和密码的代码,真令人疑惑,难道他们连接Sql Server数据库不加用户名和密码?