Qtp 简明教程

QTP - Accessing Databases

QTP 本身不提供连接到数据库的任何内置支持,但使用 VBScript 测试人员将能够使用 ADODB 对象连接到数据库并与之交互。

As such, QTP does not provide any built-in support to connect to database, however using VBScript testers will be able to connect and interact with databases using ADODB objects.

ADODB 有 4 个属性或方法,我们可以用它们来处理数据库。它们是:

ADODB has 4 properties or methods with which we will be able to work with the databases. They are −

  1. ADODB.Connection − Used to establish a connection to the Database

  2. ADODB.Command − Used to execute a SQL command(Queries or Stored Procedures)

  3. ADODB.Fields − Used to fetch a particular column from a record set after executing a query/stored proc

  4. ADODB.Recordset − Used to fetch data from a database

How to connect to Database?

数据库可以使用连接字符串进行连接。连接到各个数据库的方式有所不同。但是,可以在 www.connectionstrings.com 的帮助下构建连接字符串

Databases can be connected using Connection strings. Each database differs in the way we connect to them. However, the connection strings can be built with the help of www.connectionstrings.com

让我们看看如何使用以下参数连接到数据库−

Let us see how to connect to the database with the following parameters −

  1. Database Type − MSSQL SERVER

  2. Server Name − SQLEXPRESS

  3. Database Name − Trial

  4. User Id − sa

  5. password − Password123

查询的输出显示在 SQL Server Management Studio 中,如下所示:

The output of the Query is shown in the SQL Server Management Studio as follows −

qtp accessing db1
Dim objConnection
'Set Adodb Connection Object
Set objConnection = CreateObject("ADODB.Connection")
Dim objRecordSet

'Create RecordSet Object
Set objRecordSet = CreateObject("ADODB.Recordset")

Dim DBQuery 'Query to be Executed
DBQuery = "Select NAME from dbo.EMPLOYEE where AGE = 29"

'Connecting using SQL OLEDB Driver
objConnection.Open "Provider = sqloledb.1;Server =.\SQLEXPRESS;
User Id = sa;Password=Password123;Database = Trial"

'Execute the Query
objRecordSet.Open DBQuery,objConnection

'Return the Result Set
Value = objRecordSet.fields.item(0)
msgbox Value

' Release the Resources
objRecordSet.Close
objConnection.Close

Set objConnection = Nothing
Set objRecordSet = Nothing

Result

执行上述脚本后,输出显示在消息框中,如下所示:

On executing the above script, the output is shown in the message box as shown below −

qtp accessing db2