Original:
Http://www.cnblogs.com/Automation_software/archive/2011/06/29/2093290.html
The related function functions of C for database operations are also provided in LoadRunner:
Lr_db_connect |
Connecting to a database |
Lr_db_disconnect |
Disconnecting a database |
Lr_db_executesqlstatement |
Execute SQL statement |
Lr_db_dataset_action |
Performing operations on a database |
Lr_db_getvalue |
Retrieving values from a data set |
The syntax for each function is as follows:
Lr_db_connect ("Stepname", "connectionstring=<connection_string>", "connectionname=<connection_name>" , "connectiontype=<connection_type>", last);
Lr_db_disconnect ("stepname=<step_name>", "connectionname=<connection_name>", last);
Lr_db_executesqlstatement ("stepname=<step_name>", "connectionname=<connection_name>", "SQLStatement= <statement> ", [" datasetname=<dataset_name> ",] last);
Lr_db_dataset_action ("stepname=<step_name>", "datasetname=<dataset_name>", "Action=<action>", last);
Lr_db_getvalue ("stepname=<step_name>", "datasetname=<dataset_name>", "column=<column>", "Row= <row> "," outparam=<output_parm> ", last);
The following is an example of an Oracle database:
Action ()
{
int numrows=0;
int i=1;
Establishing a database connection
Lr_db_connect ("Stepname=databaseconnection",
"CONNECTIONSTRING=PROVIDER=ORAOLEDB.ORACLE.1; Password = test; Persist Security info=true; User ID = test; Data Source = betadb; Server = 172.19.188.191 ",
"Connectionname=invdatabaseconnection",
"Connectiontype=oledb",
last);
Execute SQL statement
NumRows = Lr_db_executesqlstatement ("Stepname=performquery",
"Connectionname=invdatabaseconnection",
"Sqlstatement=select cons_no,cons_id from C_cons where rownum<10",
"Datasetname=mydataset",
last);
Lr_output_message ("The query returned%d rows.", NumRows);
Print out results
Lr_db_dataset_action ("Stepname=printdataset",
"Datasetname=mydataset",
"Action=print",
last);
Retrieving data values using loops
while (I<10) {
Lr_db_getvalue ("Stepname=getvalue",
"Datasetname=mydataset",
"Column=cons_no",
"Row=next",
"Outparam=myoutputparam",
last);
Lr_output_message ("The value is:%s", lr_eval_string ("{Myoutputparam}"));
i=i+1; }
Release results
Lr_db_dataset_action ("Stepname=removedataset",
"Datasetname=mydataset",
"Action=remove",
last);
Disconnecting database connections
Lr_db_disconnect ("Stepname=disconnect",
"Connectionname=invdatabaseconnection",
last);
return 0;
}
After running, the output information is as follows:
Virtual User Script started
Starting action Vuser_init.
Ending action Vuser_init.
Running VUser ...
Starting Iteration 1.
Starting action action.
ACTION.C (6): DB Connection "DatabaseConnection" started ... Wait ....
ACTION.C (6): DB Connection was successful
ACTION.C: SQL Statement Execution "Performquery" started
ACTION.C (in): SQL Statement Execution was successful
ACTION.C (13): ================================================
ACTION.C: The query returned 9 rows.
ACTION.C: Dataset action "Printdataset" started
ACTION.C (20):
ACTION.C (): ******** Start Printing ***********
ACTION.C: Number of records in DataSet = 9
ACTION.C (): Num. Cons_no cons_id
ACTION.C (20): =========================================================
ACTION.C (20): 1 3096451321 310306247
ACTION.C (20): 2 3097730300 3376536
ACTION.C (20): 3 0000265443 3376537
ACTION.C (20): 4 0000265456 3376538
ACTION.C (20): 5 0000265469 3376539
ACTION.C (20): 6 0000265472 3376540
ACTION.C (20): 7 3098976354 314348099
ACTION.C (20): 8 0000265498 3376542
ACTION.C (20): 9 0000265502 3376543
ACTION.C (): ********end printing ***********
ACTION.C (20):
ACTION.C: Dataset Action was successful
ACTION.C: Get db Value "GetValue" started
ACTION.C: Get db value was successful
ACTION.C (27): ================================================
ACTION.C: The value is:3096451321
ACTION.C: Get db Value "GetValue" started
ACTION.C: Get db value was successful
ACTION.C (27): ================================================
ACTION.C: The value is:3097730300
ACTION.C: Get db Value "GetValue" started
ACTION.C: Get db value was successful
ACTION.C (27): ================================================
ACTION.C: The value is:0000265443
ACTION.C: Get db Value "GetValue" started
ACTION.C: Get db value was successful
ACTION.C (27): ================================================
ACTION.C: The value is:0000265456
ACTION.C: Get db Value "GetValue" started
ACTION.C: Get db value was successful
ACTION.C (27): ================================================
ACTION.C: The value is:0000265469
ACTION.C: Get db Value "GetValue" started
ACTION.C: Get db value was successful
ACTION.C (27): ================================================
ACTION.C: The value is:0000265472
ACTION.C: Get db Value "GetValue" started
ACTION.C: Get db value was successful
ACTION.C (27): ================================================
ACTION.C: The value is:3098976354
ACTION.C: Get db Value "GetValue" started
ACTION.C: Get db value was successful
ACTION.C (27): ================================================
ACTION.C: The value is:0000265498
ACTION.C: Get db Value "GetValue" started
ACTION.C: Get db value was successful
ACTION.C (27): ================================================
ACTION.C: The value is:0000265502
ACTION.C: Dataset action "Removedataset" started
ACTION.C (+): Dataset action was successful
ACTION.C (IN): DB disconnect "Disconnect" started
ACTION.C (IN): DB Disconnect was successful
ACTION.C (41): ================================================
Ending action action.
Ending Iteration 1.
Ending VUser ...
Starting action Vuser_end.
Ending action vuser_end.
VUser Terminated.
LoadRunner Accessing the database