恳请各位高手指点,并举例说明:用 ODBC 和 不用ODBC 连接数据库?
谢谢!!

解决方案 »

  1.   

    Option Explicit
    '数据库类型.
    Public Enum SmDbType
                Access = 1    'Access
                Excel = 2     'Excel
                Text = 3      '文本
                FoxPro = 4    'FoxPro
                dBase = 5     'dBase
                SYBASE = 6    'DB
    End Enum
    '
    '创建一个连接(连接到其它数据库类型)
    '函数名:CreateOtherConn
    '参数:  DbConnection ADODB连接,FilePath 数据库路径,UserName 登录用户名,PassWord 登录密码,DbType SmDbType枚举数据库类型
    '返回值:TRUE 连接成功.FALSE 连接失败.
    '例:
    'CreateOtherConn Cnn, "E:\CjhLx\dbf", , , FoxPro
    'StrSql = "select * from [employee.dbf]"
    'Set Rs = RsOpen(Cnn, StrSql)
    'Set DataGrid1.DataSource = RsPublic Function CreateOtherConn(ByRef DbConnection As ADODB.Connection, _
                                   FilePath As String, _
                                   Optional UserName As String = "admin", _
                                   Optional PassWord As String = "", _
                                   Optional DbType As SmDbType = Access) As Boolean
            Dim ConnStr As String
            Dim DriveName(5) As String
            Dim tDbType(5) As String
            Dim UserPwd(5) As String
            
            '驱动程序
            DriveName(1) = "{Microsoft Access Driver (*.mdb)}"
            DriveName(2) = "{Microsoft Excel Driver (*.xls)}"
            DriveName(3) = "{Microsoft Text Driver (*.txt; *.csv)}"
            DriveName(4) = "{Microsoft Visual FoxPro Driver};SourceType=DBF"
            DriveName(5) = "{Microsoft dBase Driver (*.dbf)}"
            DriveName(6) = "{Microsoft Paradox-Treiber (*.db )}"
            '{Microsoft Paradox Driver (*.db )}
            
            '类型
            tDbType(1) = "MDB"
            tDbType(2) = "XLS"
            tDbType(3) = "TXT"
            tDbType(4) = "FDB"
            tDbType(5) = "DDB"
            tDbType(6) = "DB"
            '用户名和密码.
            UserPwd(1) = "Uid=" & UserName & ";Pwd=" & PassWord & ";"
            UserPwd(2) = "Uid=" & UserName & ";Pwd=" & PassWord & ";"
            UserPwd(3) = ""
            UserPwd(4) = "Uid=" & UserName & ";Pwd=" & PassWord & ";"
            UserPwd(5) = "Uid=" & UserName & ";Pwd=" & PassWord & ";"
            UserPwd(6) = "Uid=" & UserName & ";Pwd=" & PassWord & ";"
            
            On Error Resume Next
            
            If DbConnection.State = adStateOpen And Not IsEmpty(adStateOpen) Then
               DbConnection.Close
            End If
            ConnStr = "Provider=MSDASQL.1;Persist Security Info=False;DRIVER=" & DriveName(DbType) & ";" & UserPwd(DbType) & "DBQ=" & FilePath
            DbConnection.ConnectionString = ConnStr
            DbConnection.Open
            DoEvents
              
            If Err.Number = 0 Then
               DbStyle = tDbType(DbType)
               CreateOtherConn = True
            Else
               Err.Clear
               DbStyle = ""
               CreateOtherConn = False
            End If
      End Function
      

  2.   

    ODBC Driver for Sybase 
      
    If using the Sybase System 11 ODBC Driver:oConn.Open "Driver={SYBASE SYSTEM 11};" & _
              "Srvr=myServerName;" & _
             "Uid=myUsername;" & _
              "Pwd=myPassword;"If using the Intersolv 3.10 Sybase ODBC Driver:oConn.Open "Driver={INTERSOLV 3.10 32-BIT Sybase};" & _
              "Srvr=myServerName;" & _
             "Uid=myUsername;" & _
              "Pwd=myPassword;"------------------------------------------------
    ODBC Driver for Sybase SQL Anywhere 
      
    oConn.Open "ODBC; Driver=Sybase SQL Anywhere 5.0;" & _
              "DefaultDir=c:\dbpath\;" & _
              "Dbf=c:\sqlany50\mydb.db;" & _
             "Uid=myUsername;" & _
              "Pwd=myPassword;"
              "Dsn="""";"Note: Including the DSN tag with a null string is absolutely critical or else you get the dreaded -7778 error.
      

  3.   

    用ADO连
      dim cnn as new adodb.connection
      dim rst as new adodb.recordset  cnn.connectionstring="driver{SQL 6.5};uid=sa;pwd=;database=db_name;"
      cnn.cursorlocation=aduseclient
      cnn.open
      

  4.   

    补充加dsn=ODBC_name 为ADO + ODBC 连,不加dsn则为直连