VB如何连接MYSQL数据库?

2024-12-15 12:31:40
推荐回答(2个)
回答1:

VB连接MYSQL数据的方法
博客分类:
VB
VBMySQLSQLServerSQLSocket
第一步:上网http://dev.mysql.com/downloads/connector/odbc/下载mysql-connector-odbc-5.1.6-win32.msi

第二步:安装mysql-connector-odbc-5.1.6-win32.msi

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

第四步:打开代码窗口, 在 Form_Load 过程中输入下面的代码(具体说明在代码的注释中):

Vb代码
' 定义并创建数据库连接和访问对象
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 = "root"
db_pass = ""
db_data = "erqiuda"

' 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 5.1 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

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

回答2:

VB连接SQL数据库的模块:
Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public addFlag As Boolean

Public Function OpenCn() As Boolean
Dim mag As String
On Error GoTo strerrmag
Set conn = New ADODB.Connection
conn.ConnectionTimeout = 25
conn.Provider = "sqloledb"
conn.Properties("data source").Value = "127.0.0.1" ''服务器的名字
conn.Properties("initial catalog").Value = "chart" ''库名
conn.Properties("integrated security").Value = "SSPI" ''登陆类型
conn.Open
OpenCn = True
addFlag = True
Exit Function
strerrmag:
mag = "Data can''t connect"
Call MsgBox(mag, vbOKCancel, "Error:Data connect")
addFlag = False
End
End Function

Public Sub clocn()
On Error Resume Next
If conn.State <> adStateClosed Then conn.Close
Set conn = Nothing
End Sub

Public Function openrs(ByVal strsql As String) As Boolean ''连接数据库记录集
Dim mag As String
Dim rpy As Boolean
On Error GoTo strerrmag
Set rs = New ADODB.Recordset
If addFlag = False Then rpy = True
With rs
.ActiveConnection = conn
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open strsql
End With
addFlag = True
openrs = True
Exit Function
strerrmag:
mag = "data not connect"
Call MsgBox(mag, vbOKCancel, "error:connect")
openrs = False
End
End Function
Public Sub clors()
On Error Resume Next
If rs.State <> adStateClosed Then rs.Clone
Set rs = Nothing
End Sub