QTP中VBS调用数据库的方式【mysql,sqlserver,oracle,excel】
2012-06-13 18:18
621 查看
Option Explicit Dim oDataBase Set oDataBase = new DataBase Dim sDBName, sSQL, RS '''To Read Mysql Data 'sName = "Mysql.ProductDB" 'sSQL = "select * from Products_Core limit 0,10;" 'print oDataBase.StartEngine("Mysql",sName, "ProductDB") 'Set RS = oDataBase.DB_Execute_Query(sName, sSQL) 'print oDataBase.Get_Field_Value(RS, 8, "product_id") ' '''To Read SqlServer Data 'sName = "SQLServer.Account" 'sSQL = "select top 10 * from dangdang_money" 'print oDataBase.StartEngine("SQLServer",sName, "Account") 'Set RS = oDataBase.DB_Execute_Query(sName, sSQL) 'print oDataBase.Get_Field_Value(RS, 8, "money_id") ' '''To Read Excel Data 'sName = "Excel.TD.xls" 'sSQL = "select * from publish_comm_data" 'print oDataBase.StartEngine("Excel",sName, "TD.xls") 'Set RS = oDataBase.DB_Execute_Query(sName, sSQL) 'print oDataBase.Get_Field_Value(RS, 2, "PID") ' '''To Read OracleData 'sName = "Oracle.DB" 'sSQL = "select * from Table" 'print oDataBase.StartEngine("Oracle",sName, "DB") 'Set RS = oDataBase.DB_Execute_Query(sName, sSQL) 'print oDataBase.Get_Field_Value(RS, 2, "Field") ' class DataBase Private oDBConns Private oCurrentConn Public Property Get Version Version = "0.1" End Property Public Function StartEngine(sType, sName, sDBName) Dim oDBEngine Dim bResult Dim i bResult = True On Error Resume Next Execute "Set oDBEngine = New " & sType & "Engine" If Err.Number <> 0 Then bResult = False Reporter.ReportEvent micFail, "DataEngine StartEngine", "Failed to create instance : " & sType End If On Error Goto 0 If bResult = True Then On Error Resume Next Set oDBEngine.Enginer = Me Err.Clear On Error Goto 0 oDBEngine.StartEngine(sDBName) if not oCurrentConn is Nothing then If not oDBConns.Exists(sName) Then oDBConns.Add sName, oCurrentConn Set oCurrentConn = Nothing End If else bResult = False end if End If If bResult = False Then Reporter.ReportEvent micFail, "DataBase StartEngine", "Failed to start engine " & sName & " Of Type " & sType On Error Resume Next oDBEngine.StopEngine On Error Goto 0 End If Set oDBEngine = Nothing StartEngine = bResult End Function Public Function DB_Connect(connection_string) On error Resume next set oCurrentConn = CreateObject("ADODB.Connection") If Err.Number <> 0 then Set oCurrentConn = Nothing Exit Function End If oCurrentConn.Open connection_string If Err.Number <> 0 then Set oCurrentConn = Nothing Exit Function End If On Error Goto 0 End Function Public Function DB_Disconnect(sName) if oDBConns.Exists(sName) then oDBConns(sName).close Set oDBConns(sName) = Nothing oDBConns.Delete sName end if End Function Public Function DB_Execute_Query(sName, SQL) if oDBConns.Exists(sName) then Set DB_Execute_Query = oDBConns(sName).Execute(SQL) end if End Function Public Function Get_Field_Value(RS, int_col, str_Field) index_col = int(int_col) If (not RS.eof) then Rs.MoveFirst While index_col > 1 RS.MoveNext index_col = index_col - 1 Wend Get_Field_Value = RS.Fields.Item(str_Field) If Not IsNull(Get_Field_Value) Then Get_DB_Field_Value=CDbl(Get_Field_Value) End If Else Reporter.ReportEvent micFail,"未在数据库中查询到值,请检查数据的正确性" Rs.close Set Rs = Nothing End If End Function Public Function Get_Collection_Count(rs) Dim i For each coll in rs i = i + 1 Next Get_Collection_Count = i End Function Private Sub Class_Initialize Set oDBConns = CreateObject("Scripting.Dictionary") End Sub Private Sub Class_Terminate Set oDBConns = Nothing End Sub End class Class SQLServerEngine Public Enginer Public Property Get Version Version = "0.1" End Property Public Property Get EngineType EngineType = "SQLServer" End Property Public Function StartEngine(str_db_name) Sql_Con_Str = "driver={SQL Server};Server=10.255.254.207;DATABASE=" & str_db_name & ";UID=writeuser;PWD=ddbackend;" StartEngine = Enginer.DB_Connect(Sql_Con_Str) End Function Public Sub StopEngine Set Enginer = Nothing End Sub End Class Class MysqlEngine Public Enginer Public Property Get Version Version = "0.1" End Property Public Property Get EngineType EngineType = "MysqlServer" End Property Public Function StartEngine(str_db_name) Sql_Con_Str = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=10.255.254.208;PORT=3306;DATABASE=" & str_db_name & ";USER=writeuser;PASSWORD=ddbackend;OPTION=3;" StartEngine = Enginer.DB_Connect(Sql_Con_Str) End Function Public Sub StopEngine Set Enginer = Nothing End Sub End Class Class OracleEngine Public Enginer Public Property Get Version Version = "0.1" End Property Public Property Get EngineType EngineType = "OracleServer" End Property Public Function StartEngine(str_db_name) Sql_Con_Str = "driver={Microsoft ODBC for Oracle};Server=" & str_db_name & ";UID=dangdang;PWD=dangdang;" StartEngine = Enginer.DB_Connect(Sql_Con_Str) End Function Public Sub StopEngine Set Enginer = Nothing End Sub End Class Class ExcelEngine Public Enginer Public Property Get Version Version = "0.1" End Property Public Property Get EngineType EngineType = "OracleServer" End Property Public Function StartEngine(str_db_name) Sql_Con_Str = "DBQ=D:\QTP\ProductPage_Project\Test_Data\" & str_db_name & ";Driver=Driver do Microsoft Excel(*.xls)" StartEngine = Enginer.DB_Connect(Sql_Con_Str) End Function Public Sub StopEngine Set Enginer = Nothing End Sub End Class
使用说明:
Dim oDataBase Set oDataBase = new DataBase Dim sDBName, sSQL, RS '''To Read Mysql Data 'sName = "Mysql.ProductDB" 'sSQL = "select * from Products_Core limit 0,10;" 'print oDataBase.StartEngine("Mysql",sName, "ProductDB") 'Set RS = oDataBase.DB_Execute_Query(sName, sSQL) 'print oDataBase.Get_Field_Value(RS, 8, "product_id")格式如上,但是具体的机器上链接时需要配置相应的数据库连接串,可以修改各engine里的Sql_Con_Str,其实主要修改驱动的名称,因为不同的驱动的名字不一样。
相关文章推荐
- java连接oracle,db2,MySQL,sqlserver等数据库的方式
- VBS连接数据库—oracle mysql的连接方式,测试可用
- 发布NBearLite v1.0.0 beta - 全面支持SqlServer,Oracle,MySql,PostgreSql数据库存储过程调用代码生成(C#/VB.NET)
- MySql、SqlServer、Oracle 三种数据库查询分页方式
- 四种常见数据库的连接方式:Mysql,Oracle,SQLserver,ACCESS
- 发布NBearLite v1.0.0 beta - 全面支持SqlServer,Oracle,MySql,PostgreSql数据库存储过程调用代码生成(C#/VB.NET)
- 发布NBearLite v1.0.0 beta - 全面支持SqlServer,Oracle,MySql,PostgreSql数据库存储过程调用代码生成(C#/VB.NET)
- 各种数据库(oracle、mysql、sqlserver等)在Spring中数据源的配置和JDBC驱动包 .
- sqlserver、Mysql、Oracle三种数据库的优缺点总结
- JDBC连接数据库(三种不同的数据库)(Oracle、SQLServer、MySQL)
- 各种数据库(oracle、mysql、sqlserver等)在Spring中数据源的配置和JDBC驱动包----转
- oracle mysql sqlserver 查看当前所有数据库及数据库基本操作命令
- oracle,mysql,SqlServer三种数据库的分页查询的实例
- Oracle、Db2、SqlServer、MySQL 数据库插入当前系统时间
- 各种数据库(oracle、mysql、sqlserver等)在Spring中数据源的配置和JDBC驱动包
- [C#] zdbviewcs: 跨平台数据库查看器。支持SqlServer、Oracle、MySql等数据库
- c#下各种数据库操作的封装!(支持ACCESS,SQLSERVER,DB2,ORACLE,MYSQL)(三)
- mysql,sqlserver,oracle三种数据库的大对象存取
- mysql, sqlserver 2005 express, oracle xe轻量级免费数据库比较
- MySQL、SqlServer、Oracle三大主流数据库分页查询