Download - Programming using Database Object
Why Database Automation?
All tables in the Web
Application must
populate from the database
REQUIREMENT SPECS.
Database Automation Process
Create an instance of ADO database object 1
Prepare the connection string2
Open the database connection3
Create an ADO Recordset4
Steps in Database Automation:
Create SQL query5
Open the Recordset6
Extract data from the Record set 7
Close the Recordset and Database connection8
Step 1: Creating an ADO Connection to the Database
It acts as programming interface to access data in a database.
It is a Microsoft Technology
Active X Data Objects
ADO
CreateObject(“ADODB.Connection”)
Set objDBConnection = CreateObject(“ADODB.Connection”)
Creation of ADO Connection Creating the instance of ADO - database connection object by using the CreateObject()
method
Step 2: Preparing Connection String
'Use Fields and get the record set column count when we don't ‘know the 'column count
'Create an ADO connection to a databaseSet objDBConnection = CreateObject("ADODB.Connection")
'Connection String for MSAccess Database - DSN lessstrAccessDBPath = "C:\Program Files\HP\QuickTestProfessional\samples\flight32.mdb"
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & 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 provider
For the password protected database, you also need to provide valid database User ID and Password in the connection string.
Note:
Data Source
Step 3: Opening the Database Connection
'Use Fields and get the record set column count when we don't ‘know the 'column count
'Create an ADO connection to a databaseSet objDBConnection = CreateObject("ADODB.Connection" )
'Connection String for MSAccess Database - DSN lessstrAccessDBPath = "C:\Program Files\HP\QuickTestProfessional\samples\flight32.mdb"
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strAccessDBPath
'Open the database connectionobjDBConnection.Open strConnString
To open the database, we need to use the Open method of Database object.
Step 4: Creating an ADO Recordset
'Use Fields and get the record set column count when we don't ‘know the 'column count
'Create an ADO connection to a databaseSet objDBConnection = CreateObject("ADODB.Connection" )
'Connection String for MSAccess Database - DSN lessstrAccessDBPath = "C:\Program Files\HP\QuickTestProfessional\samples\flight32.mdb"
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strAccessDBPath
'Open the database connectionobjDBConnection.Open strConnString
'Create an ADO recordsetSet objRecordSet = CreateObject("ADODB.Recordset")
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”)
Step 5: Creating SQL Query
'Use Fields and get the record set column count when we don't ‘know the 'column count
'Create an ADO connection to a databaseSet objDBConnection = CreateObject("ADODB.Connection" )
'Connection String for MSAccess Database - DSN lessstrAccessDBPath = "C:\Program Files\HP\QuickTestProfessional\samples\flight32.mdb"
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strAccessDBPath
'Open the database connectionobjDBConnection.Open strConnString
'Open the database connectionSet objRecordSet = CreateObject("ADODB.Recordset")
'SQLQuery to get the datastrQuery = "SELECT * FROM Orders WHERE Order_Number<=5"
Step 6: Opening the Recordset
'Use Fields and get the record set column count when we don't ‘know the 'column count
'Create an ADO connection to a databaseSet objDBConnection = CreateObject("ADODB.Connection" )
'Connection String for MSAccess Database - DSN lessstrAccessDBPath = "C:\Program Files\HP\QuickTestProfessional\samples\flight32.mdb"
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strAccessDBPath
'Open the database connectionobjDBConnection.Open strConnString
'Open the database connectionSet objRecordSet = CreateObject("ADODB.Recordset")
'SQLQuery to get the datastrQuery = "SELECT * FROM Orders WHERE Order_Number<=5“
'Open the recordset objRecordSet.Open strQuery,objDBConnection
Step 7: Extracting Data from the Recordset
'Open the database connectionSet objRecordSet = CreateObject("ADODB.Recordset")
'SQLQuery to get the datastrQuery = "SELECT * FROM Orders WHERE Order_Number<=5“
'Open the recordset objRecordSet.Open strQuery,objDBConnection
'Record Set has only one row - Get column count of record set using FieldsintColumnCount = objRecordSet.Fields.Count
'Read all the fields from record setFor intCol = 0 to intColumnCount-1
msgbox "Column Name:" & objRecordSet.Fields.Item(intCol).Name & vbNewLine&_ "Value:" & objRecordSet(intCol)
Next
Step 8: Closing Recordset and Database Connections
'Open the database connectionSet objRecordSet = CreateObject("ADODB.Recordset")
'SQLQuery to get the datastrQuery = "SELECT * FROM Orders WHERE Order_Number<=5“
'Open the recordset objRecordSet.Open strQuery,objDBConnection
'Record Set has only one row - Get column count of record set using FieldsintColumnCount = objRecordSet.Fields.Count
'Read all the fields from record setFor intCol = 0 to intColumnCount-1 msgbox "Column Name:" & objRecordSet.Fields.Item(intCol).Name & vbNewLine&_ "Value:" & objRecordSet(intCol)Next
'Close RecordSet and Database ConnectionsobjRecordSet.Close
objDBConnection.Close