对sql Server数据库的负载测试的脚本

上一篇 / 下一篇  2007-05-10 17:56:35 / 个人分类:LoadRunner

这段时间一直在练习“如何用ODBC协议编写对sql Server数据库的负载测试的脚本”,下面是我这几天编写的脚本,已运行通过,注意脚本中的变量都在“vdf.h”文件中定义

//使用odbc协议

Action()
{
   unsigned long i=0;
   unsigned long * const count = &i; 

//1   初始化环境
 lrd_init(&InitInfo, DBTypeVersion);

//2   open context:Ctx1
     // 初始化 LRD_CONTEXT structure
 lrd_open_context(&Ctx1, LRD_DBTYPE_ODBC, 0, 0, 0);

//3   Allocates a connection structure:con1
 lrd_alloc_connection(&Con1, LRD_DBTYPE_ODBC, Ctx1, 0 /*Unused*/, 0);

//4  Connects  to the database.链接数据库
    lrd_open_connection(&Con1, LRD_DBTYPE_ODBC, "", "", "","DRIVER=SQL Server;SERVER=;UID=;PWD=;DATABASE=", Ctx1, 1, 0);

//5   open cursor
     //the function(lrd_open_cursor) opens a cursor by setting up an LRD_CURSOR structure.
    //You can use a single cursor to execute successive SQL statements
    lrd_open_cursor(&Csr1, Con1, 0);

//7  插入一条记录并返回该条记录的关键值
    lrd_cancel(0, Csr1, 0 /*Unused*/, 0);
    lrd_stmt(Csr1,"INSERT INTO book1 (b) VALUES ('abc') Select IDENT_CURRENT('book1') \r\n", -1, 1 /*Direct exec*/, 0 /*None*/, 0);

    //一定要使用下面的函数重设置LRD_CURSOR结构,否则在执行lrd_fetch时会提示“[ODBC SQL Server Driver]无效的游标状态”,
 lrd_result_set(Csr1, 0, 0, 0);  
    //邦定列
    lrd_bind_cols(Csr1, BCInfo_D39, 0);//其中BCInfo_D39在vdf.h中定义
    //保存某行某列到变量中,在这个脚本中没有什么作用,只是为了体现这个函数的作用
 lrd_save_col(Csr1,1,1,0,"Saved_number_D34");
   
    //获取所有查询出来的值
    lrd_fetch(Csr1, -8, 1, count, PrintRow3, 0);//其中PrintRow3在print.inl中定义,其中count为本次获得的记录的总行数
    //保存当前行的某一个字段的值到变量中(最后一行)
    lrd_save_value(&a_D36, 0, 0, "emp_id"); 

//8  根据lrd_save_value所取得的最后一行的a_D36字段最为查询条件,既所插入的值所返回的关键值

    //释放Csr後
    lrd_cancel(0, Csr1, 0 /*Unused*/, 0);
    lrd_stmt(Csr1,"select * from book1 where a = {emp_id} \r\n", -1, 1 /*Direct exec*/, 0 /*None*/, 0);
    lrd_bind_cols(Csr1, BCInfo_D39, 0);
    lrd_fetch(Csr1, -8, 1, count, PrintRow3, 0);   
  
//9   close cusor、connectio、context
    lrd_cancel(0, Csr1, 0 /*Unused*/, 0);
    lrd_result_set(Csr1, 0, 0, 0);
    lrd_db_option(Csr1, OT_ODBC_CURSOR_CLOSE, 0, 0);
    lrd_db_option(Csr1, OT_ODBC_CURSOR_UNBOUNDCOLS, 0, 0);
       
    lrd_close_cursor(&Csr1, 0);
    lrd_close_connection(&Con1, 0, 0);
    lrd_free_connection(&Con1, 0 /*Unused*/, 0);
    lrd_close_context(&Ctx1, 0, 0);
}


TAG: LoadRunner

 

评分:0

我来说两句

日历

« 2024-04-25  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 7011
  • 日志数: 9
  • 建立时间: 2006-12-05
  • 更新时间: 2007-05-11

RSS订阅

Open Toolbar