Programming using Database Object

Download Programming using Database Object

Post on 02-Jan-2016

33 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

Programming using Database Object. Why Database Automation?. All tables in the Web Application must populate from the database. REQUIREMENT SPECS. Database Automation Process. Steps in Database Automation:. Prepare the connection string. Create an instance of ADO database object. - PowerPoint PPT Presentation

TRANSCRIPT

<p>Slide 1</p> <p>Programming using Database ObjectColor TemplatesWelcome to Database Automation with VB Script Lesson.1XYZ30 June 2010Why Database Automation?</p> <p> All tables in the Web </p> <p> Application must </p> <p> populate from the databaseREQUIREMENT SPECS.</p> <p>Observe this table on the Web Application being tested by QTP. The requirement document states that the table must populate with the data from the database. Now, how QTP will ensure that the data being shown in the table is the same in the database?By default, QTP doesnt have any mechanism with which it can access the database being used by the Application under Test. So we need to write VB Script programs for establishing database connectivity, importing and exporting data, etc. So, essentially we need to automate the database, so that QTP can automatically get connected to database and work with data in the database. In this lesson we will learn about Database automation using VB Scripts. </p> <p>2XYZ30 June 2010Database Automation ProcessCreate an instance of ADO database object 1Prepare the connection string2Open the database connection3Create an ADO Recordset4Steps in Database Automation:Create SQL query5Open the Recordset6Extract data from the Record set 7Close the Recordset and Database connection8The steps we need to perform for Database automation, are:Create an instance of ADO database object Prepare the connection string,Open the database connection,Create an ADO Record set ,Create S Q L query,Open the Record set, Extract data from the Record set, and Close the Record set and Database connection.</p> <p>Let us now learn each step in detail. 3XYZ30 June 2010</p> <p>Step 1: Creating an ADO Connection to the Database</p> <p>It acts as programming interface to access data in a database.It is a Microsoft TechnologyActive X Data ObjectsADOCreateObject(ADODB.Connection)Set objDBConnection = CreateObject(ADODB.Connection)Creation of ADO Connection Creating the instance of ADO - database connection object by using the CreateObject() methodBefore we learn about ADO connection, let us first understand what ADO is. </p> <p>ADO or Active X Data Objects is a Microsoft technology. It acts as programming interface to access data in a database. Now let us learn how to create an ADO connection to a database. Creation of ADO Connection means creating the instance of ADO - database connection object. So, we need to use the Create Object method. The syntax is shown on screen. We have assigned the ADO Database Connection Object to a variable. You can observe that the Set keyword is used along with the variable so that interpreter considers this variable as an object. 4XYZ30 June 2010</p> <p>Step 2: Preparing Connection String'Use Fields and get the record set column count when we don't know the 'column count</p> <p>'Create an ADO connection to a databaseSet objDBConnection = CreateObject("ADODB.Connection")</p> <p>'Connection String for MSAccess Database - DSN lessstrAccessDBPath = "C:\Program Files\HP\QuickTestProfessional\samples\flight32.mdb"</p> <p>strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &amp; strAccessDBPath It is better to store the physical path of the database in a variable.Note:Provider is Microsoft Jet OLEDB 4.0. The Provider details vary with the type of database we want to connect to, and location of the database.Note:We need to specify the providerFor the password protected database, you also need to provide valid database User ID and Password in the connection string.Note:Data SourceNow that we have created the instance of ADO Database Connection object, let us prepare the connection string. Before preparing the connection string, let us specify the path to the Database to which we want to connect to. Make a note: It is better to store the physical path of the database in a variable. Now, let us prepare the Connection string. Say, we want to connect to MS Access Database located in our system. So, as part of preparing connection string, first we need to specify the provider. In our case, the provider is Microsoft Jet OLEDB 4.0. Make a Note: The Provider details vary with the type of database we want to connect to, and location of the database.The next part in the connection string syntax is the Data Source. Data source is the path to the database, which we have already specified and stored in the variable.Make a Note: For the password protected database, you also need to provide valid database User ID and Password in the connection string.So, connection string in our case looks like the one shown on screen. We are storing the connection string in a variable, to avoid typographical issues. Lets move forward. </p> <p>5XYZ30 June 2010</p> <p>Step 3: Opening the Database Connection'Use Fields and get the record set column count when we don't know the 'column count</p> <p>'Create an ADO connection to a databaseSet objDBConnection = CreateObject("ADODB.Connection" )</p> <p>'Connection String for MSAccess Database - DSN lessstrAccessDBPath = "C:\Program Files\HP\QuickTestProfessional\samples\flight32.mdb"</p> <p>strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &amp; strAccessDBPath </p> <p>'Open the database connectionobjDBConnection.Open strConnStringTo open the database, we need to use the Open method of Database object.Now that we have prepared the connection string. Let us open the database. To open the database, we need to use Open method of Database object. The syntax for opening the database is shown on screen. 6XYZ30 June 2010</p> <p>Step 4: Creating an ADO Recordset'Use Fields and get the record set column count when we don't know the 'column count</p> <p>'Create an ADO connection to a databaseSet objDBConnection = CreateObject("ADODB.Connection" )</p> <p>'Connection String for MSAccess Database - DSN lessstrAccessDBPath = "C:\Program Files\HP\QuickTestProfessional\samples\flight32.mdb"</p> <p>strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &amp; strAccessDBPath </p> <p>'Open the database connectionobjDBConnection.Open strConnString</p> <p>'Create an ADO recordsetSet objRecordSet = CreateObject("ADODB.Recordset")</p> <p>The ADO Record set object is used to hold a set of records from a database table. A Record set object consist of records and columns (fields).CreateObject(ADODB.Recordset)Next step is to create an ADO Record set object. The ADO Record set object is used to hold a set of records from a database table. A Record set object consist of records and columns .So, for creating ADO record set object we need to use the Create Object method. The syntax is shown on screen. 7XYZ30 June 2010</p> <p>Step 5: Creating SQL Query'Use Fields and get the record set column count when we don't know the 'column count</p> <p>'Create an ADO connection to a databaseSet objDBConnection = CreateObject("ADODB.Connection" )</p> <p>'Connection String for MSAccess Database - DSN lessstrAccessDBPath = "C:\Program Files\HP\QuickTestProfessional\samples\flight32.mdb"</p> <p>strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &amp; strAccessDBPath </p> <p>'Open the database connectionobjDBConnection.Open strConnString</p> <p>'Open the database connectionSet objRecordSet = CreateObject("ADODB.Recordset")</p> <p>'SQLQuery to get the datastrQuery = "SELECT * FROM Orders WHERE Order_Number</p>

Recommended

View more >