http://www.cnblogs.com/Automation_software/archive/2011/06/29/2093290.htmlhtml
在Loadrunner中也提供了C對數據庫操做的相關功能函數:數據庫
lr_db_connectoracle |
鏈接數據庫ide |
斷開數據庫的鏈接post |
|
執行SQL語句code |
|
對數據庫執行操做htm |
|
從數據集中檢索值 |
各函數語法以下:
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 startedStarting 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 successfulAction.c(13): SQL Statement execution "PerformQuery" startedAction.c(13): SQL Statement execution was successfulAction.c(13): ================================================Action.c(18): The query returned 9 rows.Action.c(20): Dataset action "PrintDataset" startedAction.c(20): Action.c(20): ******** Start printing ***********Action.c(20): Number of records in dataset = 9Action.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 successfulAction.c(27): Get db value "GetValue" startedAction.c(27): Get db value was successfulAction.c(27): ================================================Action.c(33): The value is: 3096451321Action.c(27): Get db value "GetValue" startedAction.c(27): Get db value was successfulAction.c(27): ================================================Action.c(33): The value is: 3097730300Action.c(27): Get db value "GetValue" startedAction.c(27): Get db value was successfulAction.c(27): ================================================Action.c(33): The value is: 0000265443Action.c(27): Get db value "GetValue" startedAction.c(27): Get db value was successfulAction.c(27): ================================================Action.c(33): The value is: 0000265456Action.c(27): Get db value "GetValue" startedAction.c(27): Get db value was successfulAction.c(27): ================================================Action.c(33): The value is: 0000265469Action.c(27): Get db value "GetValue" startedAction.c(27): Get db value was successfulAction.c(27): ================================================Action.c(33): The value is: 0000265472Action.c(27): Get db value "GetValue" startedAction.c(27): Get db value was successfulAction.c(27): ================================================Action.c(33): The value is: 3098976354Action.c(27): Get db value "GetValue" startedAction.c(27): Get db value was successfulAction.c(27): ================================================Action.c(33): The value is: 0000265498Action.c(27): Get db value "GetValue" startedAction.c(27): Get db value was successfulAction.c(27): ================================================Action.c(33): The value is: 0000265502Action.c(36): Dataset action "RemoveDataset" startedAction.c(36): Dataset action was successfulAction.c(41): DB disconnect "Disconnect" startedAction.c(41): DB disconnect was successfulAction.c(41): ================================================Ending action Action.Ending iteration 1.Ending Vuser...Starting action vuser_end.Ending action vuser_end.Vuser Terminated.