1.首先安装mysql数据驱动,qtp在windows系统下操作连接mysql,所以下载mysql-connector-odbc- 5.1.8-win32.msi
Dim Conn
Set Conn=CreateObject("ADODB.Connection")
Const ConnectionString="DSN=mysql_Mail;DATABASE=extmail;PWD=123456;PORT=3306;SERVER=192.168.2.52;UID=root"
‘Const ConnectionString="Driver={Mysql ODBC 5.1 Driver};DATABASE=extmail;PWD=123456;PORT=3306;SERVER=192.168.2.52;UID=root"
Conn.Open ConnectionString
If Conn.State<>0 Then
Reporter.ReportEvent micPass,"testing","连接数据库成功"
else
Reporter.ReportEvent micFail,"testing","连接数据库失败"
End If
Dim Conn
Set Conn=CreateObject("ADODB.Connection")
‘Const ConnectionString="DSN=mysql_Mail;DATABASE=extmail;PWD=123456;PORT=3306;SERVER=192.168.2.52;UID=root"
Const ConnectionString="Driver={Mysql ODBC 5.1 Driver};DATABASE=extmail;PWD=123456;PORT=3306;SERVER=192.168.2.52;UID=root"
Conn.Open ConnectionString
If Conn.State<>0 Then
Reporter.ReportEvent micPass,"testing","连接数据库成功"
else
Reporter.ReportEvent micFail,"testing","连接数据库失败"
End If
Dim Conn,str,sql,i,sum
Set Conn=CreateObject("ADODB.Connection")‘创建数据库实例
Const ConnectionString="DSN=mysql_Mail;DATABASE=extmail;PWD=123456;PORT=3306;SERVER=192.168.2.52;UID=root"
‘Const ConnectionString="Driver={Mysql ODBC 5.1 Driver};DATABASE=extmail;PWD=123456;PORT=3306;SERVER=192.168.2.52;UID=root"
Conn.Open ConnectionString
If Conn.State<>0 Then
Reporter.ReportEvent micPass,"testing","连接数据库成功"
else
Reporter.ReportEvent micFail,"testing","连接数据库失败"
End If
Set str=CreateObject("ADODB.Recordset")‘创建数据集实例
‘查询数据库
sql="Select * from manager where type=‘postmaster‘"
str.Open sql,conn,1,1 ‘1,1表示只读;1,3表示插入数据;2,3表示修改数据
str.MoveFirst‘使游标指向第一个记录
sum=""
While Not str.EOF
‘msgbox str.Fields("username")
For i=0 to str.Fields.Count-1‘str.Fields.Count表示字段个数
sum=sum & str(i) & " " ‘把整个记录显示出来
Next
Print sum & vbCRLF‘打印所有查询的记录
str.MoveNext‘使游标进入下一个
Wend
str.Close ‘关闭数据集实例
Set str=Nothing
Conn.Close ‘关闭数据库实例
Set Conn=Nothing