• 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.

    专注于自动化、性能研究,博客为原创,转载请注明文章来源于:http://www.cnblogs.com/Automation_software/ 只求在IT界有一个清闲的世界让我静心的去专研,不求功名利禄,只为心中的那份成就感及自我成长、自我实现的快感。
  • 相关阅读:
    一定间隔时间下重复执行一个函数的几个方法
    关于 MonoDevelop on Linux 单步调试问题的解决
    MonoDevelop 4.2.2/Mono 3.4.0 in CentOS 6.5 安装笔记
    在ASP.NET MVC 4 on Mono中使用OracleClient in CentOS 6.x的问题记录
    在CentOS 6.4 x86_32中使用Rhythmbox听MP3
    MonoDevelop 4.0.9 on CentOS 6.3 安装笔记
    MemoryMappedFile 在 Mono in Linux 的开发笔记
    Mono on CentOS 6.3 安装笔记
    庆祝下:iOS 开发者企业级计划(299美元/年帐户+邓白氏码免费) 和 Windows Phone公司应用(公司帐户99美元+Symantec企业证书299美元/年))顺利发布成功
    PowerDesigner中NAME和COMMENT的互相转换,需要执行语句
  • 原文地址:https://www.cnblogs.com/Automation_software/p/2093290.html
Copyright © 2020-2023  润新知