Loadrunner 如何访问数据库

在Loadrunner中也提供了C对数据库操作的相关功能函数,以下这些数据库功能函数只能用于Web Services协议。

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.

posted on 2011-06-29 14:12  dfine.sqa  阅读(3089)  评论(12编辑  收藏  举报

导航