QTP连接数据库

'注意:其中DSN=数据源名;UID=用户名;PWD=用户密码

Dim Conn
Set Conn=CreateObject("ADODB.Connection")
Const ConnectionString="DSN=test;DATABASE=aaa;PWD=123456;PORT=3306;SERVER=172.18.4.125;UID=root"
'Const ConnectionString="Driver={Mysql ODBC 5.1 Driver};DATABASE=test;PWD=root;PORT=3306;SERVER=localhost;UID=root"
Conn.Open ConnectionString
If Conn.State<>0Then
Reporter.ReportEvent micPass,"testing","连接数据库成功"
else
Reporter.ReportEvent micFail,"testing","连接数据库失败"
End If

--------------------------------------------

Set wshNetWork = createobject("WScript.Network")
msgbox wshNetWork.ComputerName
Set wshNetWork=nothing

'把 excel 当成数据库使用


sqlConnection="DSN=Excel Files;DBQ=e:1.xlsx;"
 sqlCommand="select * from [Sheet1$]"
Set oExcel=createobject("adodb.connection")
oExcel.Open sqlConnection
Set oRst=oExcel.Execute(sqlCommand)
msgbox oRst.Fields("name")

oRst.MoveNext

msgbox oRst.Fields("name")

oExcel.Close
Set oRst=nothing
Set oExcel=Nothing

原文地址:https://www.cnblogs.com/duyy/p/3592219.html