最近学数据库编程,发现一篇好文章: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:CjhLxdbf", , , 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_MACHINESOFTWAREODBCODBCINST.INIODBC 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"