登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

面包会有的

... ...

 
 
 

日志

 
 

VB中打开常用数据库  

2007-08-21 17:09:25|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
'数据库类型.
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"
  评论这张
 
阅读(457)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018