补充:因为在ADO中DBF数据库只能用ODBC连接,所以现在想根据DBF库路径用VB创建一个DBF库的DSN,最好给出代码。谢谢。

解决方案 »

  1.   

    ----------------------------------------------------------------------
    建立ODBC,利用ODBC api添加dns方法.
          'Constant Declaration
          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
    Private Declare Function SQLAllocEnv Lib "odbc32.dll" (phenv&) As Integer
          'Function Declare
              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
          Option ExplicitPrivate Sub Command1_Click()
              Dim intRet As Long
          Dim strDriver As String
          Dim strAttributes As String      
          strDriver = "Microsoft Access Driver (*.mdb)"
          'Set the attributes delimited by null.
          'See driver documentation for a complete list of attributes.
          strAttributes = "DSN=DSN_TEMP" & Chr$(0)
          'To show dialog, use Form1.Hwnd instead of vbAPINull.
          intRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN,
    strDriver, strAttributes)
          If intRet Then
              MsgBox "DSN Deleted"
          Else
              MsgBox "Delete Failed"
          End IfEnd SubPrivate Sub Command2_Click()
    Dim intRet As Long
          Dim strDriver As String
          Dim strAttributes As String
          Dim rc As Integer 'ODBC 函 数 的 返 回 码
          Dim henv As Long 'ODBC 环 境 句 柄
    rc = SQLAllocEnv(henv) ' 获 取ODBC 环 境 句 柄      
          strDriver = "Microsoft Access Driver (*.mdb)"
          strAttributes = "SERVER=SomeServer" & Chr$(0)
          strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)
          strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr$(0)
          strAttributes = strAttributes & "DBQ=d:\pdzl.mdb" & Chr$(0)
          strAttributes = strAttributes & "UID=admin" & Chr$(0)
          strAttributes = strAttributes & "PWD=" & Chr$(0)
          'To show dialog, use Form1.Hwnd instead of vbAPINull.
          intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, _
          strDriver, strAttributes)
          If intRet Then
              MsgBox "DSN Created"
          Else
              MsgBox "Create Failed"
          End If
    End Sub
      

  2.   

    我会mdb的,应该差不多;你看看
    Option Explicit
    Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv&, ByVal fDirection%, ByVal szDSN$, ByVal cbDSNMax%, pcbDSN%, ByVal szDescription$, ByVal cbDescriptionMax%, pcbDescription%) As Integer
    Private Declare Function SQLAllocEnv% Lib "ODBC32.DLL" (env&)
    Const SQL_SUCCESS As Long = 0
    Const SQL_FETCH_NEXT As Long = 1
    Public shujupath As String
    '用代码配置mdb数据库
    Sub info()
    Dim strdescription, strattributes
    On Error GoTo aa
    Dim lg As Long
    Dim de As DBEngine
    Set de = New DBEngine
    strdescription = "试验数据源" '描述
    Dim aa As String
    aa = appstr & "db\db.mdb"
    strattributes = "dbq=" & aa & _
                   vbCr & "description=" & strdescription & _
                   vbCr & "Fil=ms access" & _
                   vbCr & "uid=" & "password="
                   
    de.RegisterDatabase "shuju", "microsoft access driver (*.mdb)", True, strattributes
    Exit Sub
    aa:
     MsgBox "调试失败"
    End Sub