怎么理解一下代码
Dim con As New ADODB.Connection
Dim db_host, db_data, db_user, db_pass, sql As String
db_host = "localhost"
db_user = "root"
db_pass = "wayne119"
db_data = "wayne"
con.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" & _
"SERVER=" & db_host & ";" & _
"DATABASE=" & db_data & ";" & _
"UID=" & db_user & ";PWD=" & db_pass & ";" & _
"OPTION=3;stmt=SET NAMES GB2312"
con.Open
Dim rs As New ADODB.Recordset
sql = "select * from student"
rs.Open sql, con
Dim i As Integer
For i = 1 To rs.Fields.Count
MsgBox i
Next i
con.Close
[解决办法]
Dim con As New ADODB.Connection ’申明一个ADO数据库连接对象
Dim db_host, db_data, db_user, db_pass, sql As String ‘分别定义4个字符串变量,用于进行数据库连接参数。
db_host = "localhost" ’本地服务器
db_user = "root"‘用户名
db_pass = "wayne119"’密码
db_data = "wayne"‘数据库
'给连接对象的连接字符串进行赋值,连接到指定的服务器
con.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" & _
"SERVER=" & db_host & ";" & _
"DATABASE=" & db_data & ";" & _
"UID=" & db_user & ";PWD=" & db_pass & ";" & _
"OPTION=3;stmt=SET NAMES GB2312"
con.Open ‘打开连接
Dim rs As New ADODB.Recordset ’申明一个记录集对象
sql = "select * from student" ’给SQL变量进行赋值,打开学生这个表,进行读取数据
rs.Open sql, con ’执行Sql语句,将返回的记录保存到记录集中
Dim i As Integer ‘定义一个整数变量
For i = 1 To rs.Fields.Count ’对记录说进行循环
MsgBox i ’显示I的值
Next i
con.Close ’关闭连接对象