How to execute sql query using qtp ? Explain with sample code.

Questions by kalpeshprajapati   answers by kalpeshprajapati

Showing Answers 1 - 4 of 4 Answers

amarreddy79

  • Feb 16th, 2007
 

i hope the following code is usefull to you............

SQL="SELECT * FROM ORDERS"

connection_string="QT_Flight32"

 isConnected = db_connect ( curConnection ,connection_string )

If isConnected = 0 then ' execute the basic SQL statement

set myrs=db_execute_query( curConnection , SQL ) ' report the query and the connection string Reporter.ReportEvent micInfo ,"Executed query and created recordset ","Connection_string is ==> " & connection_string & " SQL query is ===> " & SQL ' show the number of rows in the table using a record set msgBox " Quantity of rows in queried DB ( db_get_rows_count )==> " & db_get_rows_count( myrs ) ' show the number of rows in the table using a new SQL statement msgBox " Quantity of rows in queried DB (db_get_rows_count_SQL ) ==> " & db_get_rows_count_SQL( curConnection , "SELECT COUNT(*) FROM ORDERS" ) ' change a value of a field in an existing row rc = db_set_field_value (curConnection, "ORDERS" , "Agents_Name" , "test", "Agents_Name", "AGENT_TESTER") ' examples of how to retrieve values from the table msgBox "val row 0 col 0: " & db_get_field_value( myrs , 0 , 0 ) msgBox "val row 0 col 1: " & db_get_field_value( myrs , 0 , 1 ) msgBox "val row 1 col Name: " & db_get_field_value( myrs , 1 , "Agents_Name" ) msgBox "val SQL row 1 col Name: " & db_get_field_value_SQL( curConnection , "ORDERS" , 1 , "Agents_Name" ) db_disconnect curConnectionEnd If

  Was this answer useful?  Yes

Using ADODB connection we can connect to DBset

DBobj=createobject("ADODB.connection")

DBobj.open("DSN=raghu")

set rc=createobject("ADODB.recordset")

set rc=DBobj.execute("Select * from orders")

while rc.eof<>true value1=rc.fields(0)value2=rc.fields(1)

wend

 if you don't have dsn created then you can use connection

stringDBobj.open("drive=sqlserver;server=system20;database=raghu;uid=admin;pwd=hyderabad007")

if you have any doubts feel free to contact me for more information or FAQ's on QTP raghu_13_sa@yahoo.com or 9866379523

  Was this answer useful?  Yes

bosjerr

  • May 7th, 2008
 

'Connecting to a MS Access database
'------------------------------------------------------------------------------------------------------------

'1. Create a connection object
'2. Call the open method of the connection object

'The Connection object: Opens a session with a database and executes a query.
'The Connection object can be used to access any database with an Open Database
'Connectivity (ODBC) Driver

'CreateObject Function
'Creates and returns a reference to an Automation object.


Set objDB = CreateObject("ADODB.Connection")

'------------------------------------------------------------------------------------------------------------

'open a session to a db
'Use the ConnectionString property of a connection object to provide information 'about 'the database.
'Using this ConnectionString property, The open method connects to a databa
se

objDB.ConnectionString = "DSN=Flight32_TestData"
objDB.Open

'------------------------------------------------------------------------------------------------------------

'If this message appears:
'[Microsoft][ODBC Driver Manager] Data source name not found and no default driver 'specified
'Then:
'Control Panel
'Admistrator
'source (ODBC)
'Tab System DSN
'Add
'- Microsoft Access Driver (.mdb)
'-  Choose the name  Flight32_TestData (Same as in the ConnectionString)
'OK

'------------------------------------------------------------------------------------------------------------

'Executing a SQL query

'After you estabish a connection with a database, 
'you run a SQL query against the database
'You use the Execute method of the Connection
'object to retrieve data from a database

'The Execute method accepts a SQL statement as an input
'and returns a RecordSet object when the run completes.
'The following example shows how to use the Execute method

strQuery = "Select * from flights where departure='San Francisco'"
Set objResults = objDB.Execute(strQuery)


'------------------------------------------------------------------------------------------------------------
'Examining the Query Results

'After you execute a SQL query, you use the RecordSet object
'to examine the query results
'You can use the following properties and methods to examine the query results

'Thes properties determine if you are at the boudaries of the RecordSet Object:
'- Beginning of File (BOF)
'- End of File (EOF)

'MoveNext       - moves one record forward
'MovePrevious - moves one record backward
'Move              - moves multiple records for-/backward

'Fields.count  -  indicates the number of columns of data returned by the SQL query

'Fields("MyColumn")         -   returns the value saved in the specified column of the of 'the 'current RecordSet
'Fields.item("MyColumn").Value  - returns the value saved in the specified column of 'the 'of the current RecordSet

msgbox objResults.Fields.count
 
Do Until objResults.EOF

  strFlight           = objResults.Fields("Flight_Number")
  strDestination = objResults.Fields("Arrival")
  strAirlines       = objResults.Fields("Airlines")

  msgbox strFlight & " " & " "& strDestination & " "& strAirlines
  'do something with the data
   objResults.MoveNext
Loop

'------------------------------------------------------------------------------------------
'Closing the Database Session

objResults.Close
objDB.Close

Set objResults = Nothing
Set objDB = Nothing


  Was this answer useful?  Yes

nina

  • Aug 13th, 2009
 

 Set dbSO3 = CreateObject("ADODB.Connection") ' Create Connection  Object

 dbSO3.ConnectionString  =  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=S:Information ServicesSO.mdb"
 dbSO3.Open 
 intUserId = 11001100
' qSql4 = "SELECT *  from ActualUserIDs where user_id = "&intUserId  ' Read data from the data table

 qSql4 = "SELECT count (*)  from ActualUserIDs where user_id = "&intUserId  ' Read data from the data table
 Set objGetValue4 = dbSO3.Execute (qSql4)
 strActualUserExpirationDate = objGetValue4.Fields("user_expiration_date")
 MsgBox  strActualUserExpirationDate
'
 objGetValue4.Close
 dbSO3.Close
 Set objGetValue4 = Nothing
 Set dbSO3 = Nothing

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions