版权声明:本文为博主原创文章,未经博主允许不得转载。欢迎访问我的博客 https://blog.csdn.net/smooth00/article/details/68486247
Loadrunner与SQL Server的操作可以通过录制的方式来实现,但本文还是通过直接调用loadrunner本身的function来实现sql语句的操作,主要用到的是lr_db_connect和lr_db_executeSQLStatement两个函数,具体的用法这里不做解释,请自行查看loadrunner的帮助说明。
1、脚本协议:选择web service
在函数lr_db_connect的帮助说明中有句“Important: This function is available only from within Web Services scripts.”。这句话决定了,我们在协议选择的时候只能选择Web Services协议。
2、脚本
Action()
{
int NumRows=0;
double times=0;
int i=1;
//建立连接,ConnectionType=SQL
lr_db_connect("StepName=DatabaseConnection",
"ConnectionString=Data Source=172.16.1.215;Initial Catalog=LayIM;Persist Security Info=True;User ID=sa;Password=sa",
"ConnectionName=MyCon",
"ConnectionType=SQL",
LAST );
/*建立连接,ConnectionType=OLEDB
lr_db_connect("StepName=Connect",
"ConnectionString=Provider=SQLOLEDB;Data Source=.;Initial Catalog=my_test;User Id=sa;Password=123456",
"ConnectionName=MyCon",
"ConnectionType=OLEDB", //ConnectionType=SQL时,ConnectionString不可以带有Provider;ConnectionType=OLEDB时,必须带Provider
LAST);
*/
//执行SQL
lr_start_transaction("SQL查询");
NumRows = lr_db_executeSQLStatement("StepName=PerformQuery",
"ConnectionName=MyCon",
// 数据库语句
"SQLStatement=SELECT * FROM layim_user ORDER BY id DESC ",
"DatasetName=MyDataSet",
LAST );
times=lr_get_transaction_duration("SQL查询");
lr_end_transaction("SQL查询", LR_AUTO);
lr_output_message("The query returned %d rows.", NumRows);
lr_output_message("SQL查询语句执行时间 %f ", times);
//打印查询结果
/*lr_db_dataset_action("StepName=PrintDataset",
"DatasetName=MyDataSet",
"Action=PRINT",
LAST );*/
//获取当前第一条记录
lr_db_getvalue("StepName=GetValue",
"DatasetName=MyDataSet",
"Column=nickname",
"Row=current",
"OutParam=MyOutputParam",
LAST );
// 输出当前查询记录
lr_output_message("The value is: %s", lr_eval_string("{MyOutputParam}") );
while (i<=10) {//输出10条记录
lr_db_getvalue("StepName=GetValue",
"DatasetName=MyDataSet",
"Column=nickname",
"Row=next",
"OutParam=MyOutputParam",
LAST);
lr_output_message("The value %d is: %s", i, lr_eval_string("{MyOutputParam}") );
i=i+1;
}
lr_output_message("The query returned %d rows.", NumRows);
//释放结果
/*lr_db_dataset_action("StepName=RemoveDataset",
"DatasetName=MyDataSet",
"Action=REMOVE",
LAST);*/
lr_start_transaction("SQL插入");
NumRows = lr_db_executeSQLStatement("StepName=Insert",
"ConnectionName=MyCon",
// Insert语句,本例中第一列id是自增主键
"SQLStatement=Insert into layim_user values ('test','123456','test888888','','/test/test',getdate(),'111111',0) ",
"DatasetName=MyDataSet",
LAST );
times=lr_get_transaction_duration("SQL插入");
lr_end_transaction("SQL插入", LR_AUTO);
lr_output_message("The insert date %d rows.", NumRows);
lr_output_message("SQL插入语句执行时间 %f ", times);
//关闭链接
lr_db_disconnect("stepname=Disconnect","connectionname=MyCon",LAST);
return 0;
}
3、补充说明:
(1)ConnectionString:本文给了ConnectionType为sql和OLEDB的例子,具体数据源的连接可参见:https://www.connectionstrings.com/sql-server/
(2)ConnectionType=OLEDB时ConnectionString后面必须指定Provider属性,ConnectionType=SQL又绝对不可以指定Provider属性。不知道为什么这么要求。
(3)lr_db_connect中的ConnectionName可以随意指定,但是lr_db_executeSQLStatement中的ConnectionName必须和其保持一致。