首页 | 社区 | 博客 | 招聘 | 文章 | 新闻 | 下载 | 读书 | 代码
亲,您未登录哦! 登录 | 注册

VB中打开常用数据库

打印文章

分享到:
'数据库类型.
Public Enum SmDbType
            Access = 1    'Access
            Excel = 2     'Excel
            Text = 3      '文本
            FoxPro = 4    'FoxPro
            dBase = 5     'dBase
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 = Rs

Public 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)}"
        '类型
        tDbType(1) = "MDB"
        tDbType(2) = "XLS"
        tDbType(3) = "TXT"
        tDbType(4) = "FDB"
        tDbType(5) = "DDB"
        '用户名和密码.
        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 & ";"
        
        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


上面的只是常见的数据库类型。当然,只要你愿意,你可以在ODBC 或注册表里将 驱动程序 继续添加上去。例如我的电脑上有的驱动程序如下:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers]
"SQL Server"="Installed"
"Microsoft Access Driver (*.mdb)"="Installed"
"Microsoft Text Driver (*.txt; *.csv)"="Installed"
"Microsoft Excel Driver (*.xls)"="Installed"
"Microsoft dBase Driver (*.dbf)"="Installed"
"Microsoft Paradox Driver (*.db )"="Installed"
"Microsoft Visual FoxPro Driver"="Installed"
"Microsoft FoxPro VFP Driver (*.dbf)"="Installed"
"Microsoft dBase VFP Driver (*.dbf)"="Installed"
"Microsoft Access-Treiber (*.mdb)"="Installed"
"Microsoft Text-Treiber (*.txt; *.csv)"="Installed"
"Microsoft Excel-Treiber (*.xls)"="Installed"
"Microsoft dBase-Treiber (*.dbf)"="Installed"
"Microsoft Paradox-Treiber (*.db )"="Installed"
"Microsoft Visual FoxPro-Treiber"="Installed"
"Driver do Microsoft Access (*.mdb)"="Installed"
"Driver da Microsoft para arquivos texto (*.txt; *.csv)"="Installed"
"Driver do Microsoft Excel(*.xls)"="Installed"
"Driver do Microsoft dBase (*.dbf)"="Installed"
"Driver do Microsoft Paradox (*.db )"="Installed"
"Driver para o Microsoft Visual FoxPro"="Installed"
"Microsoft ODBC for Oracle"="Installed"
"Microsoft FoxPro Driver (*.dbf)"="Installed"
"INTERSOLV InterBase ODBC Driver (*.gdb)"="Installed"

本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )

编程爱好者论坛

本栏最新文章