ProgramFan.com
回到首页 设为首页 加入收藏 网站留言
人才招聘频道
编程爱好者BLOG
编程爱好者论坛
首页 | 论坛 | BLOG | 人才招聘 | 书评 | 文章 | 资讯 | 下载 | 源码 | 项目交易 | 兴趣小组 | 网友作品 | 资源共享 | 收藏夹 | ACM题库 | VBAPI查询 | 刻盘服务
 您所在的位置:编程爱好者网站文档中心Visual Basic文章 - 正文
 

VB中打开常用数据库

(加入日期:2004-2-12)
收藏文章】【对此文发表评论】【保存文章至硬盘】【打印文章】【字体:


'数据库类型.
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"
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( ProgramFan.Com )

对此文发表评论】 【编程爱好者论坛】 【关闭窗口

 
 
 
 
 
 

最新招聘信息

 
 


关于本站 - 网站导航 - 广告服务 - 诚邀加盟 - 联系站长友情链接赞助本站
Copyright© 1999-2008 Programfan.com. All Rights Reserved
网站制作&维护:Hannibal    Email: webmaster@pfan.cn