QTP tool itself with data query and validation function, but the use of people will feel that this function is more cumbersome, flexibility and reusability is low, so we chose to use the script to invoke the ADO component implementation of the database operation, now summarizes the work of the database operation, the code is as follows:
First, the most common database value operation
' Get database query statement
Sql= "SELECT * from user where name= ' Wangwei '"
' Get database connection characters
strconn= "Driver={microsoft ODBC for ORACLE};SERVER=ORACS177;UID=C1;PWD=C1"
' Create database Connection object
Set Sqlobj=createobject ("Adodb.connection")
' Open database with data connection string
Sqlobj.open strconn
' Executes the SQL statement and returns the corresponding result set
Set Sqlresult=sqlobj.execute (SQL)
Sqlresult.movefirst
' Get the second value of the first row in the result set
Username=sqlresult.fields (1)
' Shut down the database
Sqlobj.close
' Release database objects
Set sqlobj=nothing
Second, the occasional use of the database modification operations (sometimes used for data restoration, we work useless, but a way, so summed up here)
' Get Database modification statement
sql= "Update user set age=100 where name= ' Xiaohong '"
' Get database connection characters
strconn= "Driver={microsoft ODBC for ORACLE};SERVER=ORACS177;UID=C1;PWD=C1"
' Create database Connection object
Set Sqlobj=createobject ("Adodb.connection")
' Open database with data connection string
Sqlobj.open strconn
' Execute SQL statement
Sqlobj.execute (SQL)
' Shut down the database
Sqlobj.close
' Release database objects
Set sqlobj=nothing
Description: ADO is a COM component that can access and manipulate a data source, and developers can make simple operations on the database by invoking the component in the form of a script.
1.Connection objects
The first major object of ADO, which mainly governs the connection and shutdown function of the database, connects the database through it and the corresponding connection string, and the first step of ADO operation. Where the object has an Execute method, which can also perform operations on the database directly with the SQL statement. and is able to return the result of its execution, the object returned is the Recordset object.
2.Command objects
The main object is to complete the execution of the SQL statement, including query statements, UPDATE statements, create statements, DELETE statements, and stored procedures.
3.RecordSet objects
The object is to hold the data result set after execution. When an execution statement is executed and returned to the Recordset object, the Recordset object includes all of the current execution result sets, and the output can be looped through the EOF method, where fields is a property of the Recordset object. You can get the value you want in the result set by the value of the property.
Database Operations (ADO)