1. 程式人生 > >Loadrunner 訪問數據庫

Loadrunner 訪問數據庫

value starting 利用 rem connect exe 執行 tro vuser

原文:

http://www.cnblogs.com/Automation_software/archive/2011/06/29/2093290.html

在Loadrunner中也提供了C對數據庫操作的相關功能函數:

lr_db_connect

連接數據庫

lr_db_disconnect

斷開數據庫的連接

lr_db_executeSQLStatement

執行SQL語句

lr_db_dataset_action

對數據庫執行操作

lr_db_getValue

從數據集中檢索值

各函數語法如下:

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);

下面以oracle數據庫為例:

技術分享圖片
Action()
{
int NumRows=0;
int i=1;

//建立數據庫連接
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);

//執行SQL語句
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);

//打印出結果
lr_db_dataset_action("StepName=PrintDataset",
"DatasetName=MyDataset",
"Action=PRINT",
LAST);


//利用循環檢索數據值
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; }


//釋放結果
lr_db_dataset_action("StepName=RemoveDataset",
"DatasetName=MyDataset",
"Action=REMOVE",
LAST);


//斷開數據庫連接
lr_db_disconnect("StepName=Disconnect",
"ConnectionName=InvDatabaseConnection",
LAST);

return 0;
}
技術分享圖片

運行後,輸出信息如下:

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(13): SQL Statement execution "PerformQuery" started
Action.c(13): SQL Statement execution was successful
Action.c(13): ================================================
Action.c(18): The query returned 9 rows.
Action.c(20): Dataset action "PrintDataset" started
Action.c(20):
Action.c(20): ******** Start printing ***********
Action.c(20): Number of records in dataset = 9
Action.c(20): 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(20): ********End printing ***********
Action.c(20):
Action.c(20): Dataset action was successful
Action.c(27): Get db value "GetValue" started
Action.c(27): Get db value was successful
Action.c(27): ================================================
Action.c(33): The value is: 3096451321
Action.c(27): Get db value "GetValue" started
Action.c(27): Get db value was successful
Action.c(27): ================================================
Action.c(33): The value is: 3097730300
Action.c(27): Get db value "GetValue" started
Action.c(27): Get db value was successful
Action.c(27): ================================================
Action.c(33): The value is: 0000265443
Action.c(27): Get db value "GetValue" started
Action.c(27): Get db value was successful
Action.c(27): ================================================
Action.c(33): The value is: 0000265456
Action.c(27): Get db value "GetValue" started
Action.c(27): Get db value was successful
Action.c(27): ================================================
Action.c(33): The value is: 0000265469
Action.c(27): Get db value "GetValue" started
Action.c(27): Get db value was successful
Action.c(27): ================================================
Action.c(33): The value is: 0000265472
Action.c(27): Get db value "GetValue" started
Action.c(27): Get db value was successful
Action.c(27): ================================================
Action.c(33): The value is: 3098976354
Action.c(27): Get db value "GetValue" started
Action.c(27): Get db value was successful
Action.c(27): ================================================
Action.c(33): The value is: 0000265498
Action.c(27): Get db value "GetValue" started
Action.c(27): Get db value was successful
Action.c(27): ================================================
Action.c(33): The value is: 0000265502
Action.c(36): Dataset action "RemoveDataset" started
Action.c(36): Dataset action was successful
Action.c(41): DB disconnect "Disconnect" started
Action.c(41): 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 訪問數據庫