admin 发表于 2007-10-20 12:38:21

用VB连接MySQL数据库 — 安装ODBC

最近研究的东西中, 有一部分涉及到用VB访问MySQL数据库, 今天研究了一下小有收获, 共享出来供大家参考

首先下载 MySQL 的 ODBC 驱动, 我下载的是 MySQL ODBC 3.51 without installer 版, 下载后是一zip包, 随便解压到哪, 然后运行 cmd, 定位路径到解压的目录, 然后运行 install.bat 安装即可, 具体看下面的代码(其中以#开头的为我加的注释):
# 下文中路径都是假设的
# 需要修改成你的路径
x:
cd x:\your\mysqlODBC\path
# 直接运行 install.bat
install.bat
# output
"+---------------------------------------------------–+"
"| Install.bat |"
"+---------------------------------------------------–+"
"| |"
"| DESCRIPTION |"
"| |"
"| Use this to copy the driver and supporting files |"
"| to the system directory and register the driver. |"
"| |"
"| You can not properly install the debug version |"
"| without first installing the regular version. |"
"| |"
"| SYNTAX |"
"| |"
"| Install <debug> |"
"| |"
"| <debug> must be; |"
"| 0 - to install a regular build |"
"| 1 - to install a debug version |"
"| |"
"+---------------------------------------------------–+"
# 提示我们首次安装不能是 debug 版
# 这里需要我们加上参数, 0 是标准版, 1 是 debug 版
# 我们运行:
install.bat 0
# output
已复制 1 个文件。
已复制 1 个文件。
已复制 1 个文件。
已复制 1 个文件。
已复制 1 个文件。
已复制 1 个文件。
已复制 1 个文件。
doc\myodbc3.hlp
已复制 1 个文件。
[.\myodbc3i.c] Driver registered. Usage count is 1. Location "X:\WINDOWS\system32"
"+---------------------------------------------------–+"
"| DONE |"
"+---------------------------------------------------–+"
"| |"
"| Hopefully things went well; the Connector/ODBC |"
"| files have been copied to the system directory |"
"| and the driver has been registered. |"
"| |"
"| Connector/ODBC is ready to use. |"
"| |"
"| The most common thing to do next is to go to the |"
"| Control Panel and find the ODBC Administrator - |"
"| then use it to create a Data Source Name (DSN) |"
"| so you (and your application) can connect to a |"
"| MySQL server. |"
"| |"
"+---------------------------------------------------–+"
# 安装完成

至此, MySQL ODBC 就安装完成了, 运行 控制面板->管理工具->数据源(ODBC) 进入”驱动程序”标签页就可以看到刚刚安装的 MySQL ODBC 驱动了(如图).

http://www.langke.org/uploads/200710/16_080942_mysql.odbc.png


在尝试连接数据库之前, 需要简单说明一下连接数据库的两种最常见的方式: 一种是通过建立 DSN 来连接, 这种比较麻烦, 程序移植性差; 我一般采取利用 ADODB 连接数据库的方式, 这种比较灵活方便, 不需额外工作, 方便程序移植.

下面的例子就是通过 ADODB 连接 MySQL 数据库:

首先, 运行VB并新建一标准EXE工程, 通过菜单 工程->引用 打开”引用”对话框, 找到 Microsoft ActiveX Data Objects x.x Library , 其中 x.x 是版本号, 可能会有很多个, 这里我选择的是 2.5(如图)

http://www.langke.org/uploads/200710/16_081012_mysql.ado.png

单击”确定”关闭对话框, 这样就在工程中引入了 ado(ActiveX Data Objects), 然后双击Form设计窗体, 打开代码窗口, 在 Form_Load 过程中输入下面的代码(具体说明在代码的注释中):
' 定义并创建数据库连接和访问对象
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

' 定义数据库连接字符串变量
Dim strCn As String

' 定义数据库连接参数变量
Dim db_host As String
Dim db_user As String
Dim db_pass As String
Dim db_data As String

' 定义 SQL 语句变量
Dim sql As String

' 初始化数据库连接变量
db_host = "localhost"
db_user = "yourUsername"
db_pass = "yourPassword"
db_data = "yourDatabase"

' MySQL ODBC 连接参数
'+------------+---------------------+----------------------------------+
'| 参数名 | 默认值 | 说明 |
'+------------+------------------------------------------------------–+
'| user | ODBC (on Windows) | MySQL 用户名 |
'| server | localhost | MySQL 服务器地址 |
'| database | | 默认连接数据库 |
'| option | 0 | 参数用以指定连接的工作方式 |
'| port | 3306 | 连接端口 |
'| stmt | | 一段声明, 可以在连接数据库后运行 |
'| password | | MySQL 用户密码 |
'| socket | | (略) |
'+------------+---------------------+----------------------------------+

' 详细查看官方说明
' http://dev.mysql.com/doc/refman/5.0/en/myodbc-configuration-connection-parameters.html

strCn = "DRIVER={MySQL ODBC 3.51 Driver};" & _
"SERVER=" & db_host & ";" & _
"DATABASE=" & db_data & ";" & _
"UID=" & db_user & ";PWD=" & db_pass & ";" & _
"OPTION=3;stmt=SET NAMES GB2312"

' stmt=SET NAMES GB2312
' 这句是设置数据库编码方式
' 中文操作系统需要设置成 GB2312
' 这样中文才不会有问题
' 版本要求 mysql 4.1+

' 连接数据库
cn.Open strCn
' 设置该属性, 使 recordcount 和 absolutepage 属性可用
cn.CursorLocation = adUseClient

' 访问表 table1
sql = "select * from table1"
rs.Open sql, cn
MsgBox rs.recordCount


很简单吧, 剩下的就和操作其他数据库一样了, 最大的区别就在于一些 SQL 语句上.

BTW: 同时我发现 rs.recordset 属性返回 -1,(已解决: cn.CursorLocation = adUseClient) 还有更多更具体的问题有待研究, 这些将会在下一篇文章中做更具体的介绍
页: [1]
查看完整版本: 用VB连接MySQL数据库 — 安装ODBC