51Testing软件测试论坛

标题: 那位高手能举一个数据库调用的例子 [打印本页]

作者: fish_yy    时间: 2005-12-9 11:20
标题: 那位高手能举一个数据库调用的例子
那位高手能举一个数据库调用的例子,如已经建立了一个odbc数据源名为test(access),如何建立连接,执行sql语句,返回结果,谢谢。
作者: fish_yy    时间: 2005-12-9 15:21
版主帮帮忙啊,查不到相应的资料,求助同行们,呵呵
作者: happypeach    时间: 2005-12-11 15:42
/* -------------------------------------------------------------------------------
        Script Title       : ODBC Script Example
        Script Description :
                        
                        
        Recorder Version   : 1008
   ------------------------------------------------------------------------------- */

vuser_init()
{
//1        init
        lrd_init(&InitInfo, DBTypeVersion);

//2        open context :ctx2       
        lrd_open_context(&Ctx2, LRD_DBTYPE_ODBC, 0, 0, 0);
        lrd_db_option(Ctx2, OT_ODBC_OV_ODBC3, 0, 0);


//3         Allocates a connection structure:con2
        lrd_alloc_connection(&Con2, LRD_DBTYPE_ODBC, Ctx2, 0 /*Unused*/, 0);

//4        db option
        lrd_db_option(Con2, OT_ODBC_LOGIN_TIMEOUT, (void FAR const *)15, 0);
        lrd_db_option(Con2, OT_ODBC_SS_QUOTED_IDENT, "OFF", 0);
        lrd_db_option(Con2, OT_ODBC_SQL_PACKET_SIZE, (void FAR const *)4096, 0);
        lrd_db_option(Con2, OT_ODBC_SS_ANSI_NPW, "ON", 0);

//4        open_connection con2.
        lrd_open_connection(&Con2, LRD_DBTYPE_ODBC, "", lr_decrypt("439bcec4e"), "", lr_decrypt("439bcec47d1a6c4f5cb1a621ba48147321e9d548ffd0593b"
        "1ba1c5097e22f8bec49be99e63rrb76f20f5d7a8692187020d432b8e"
        "ed83601279b6ab6bff5233550fec251642bea0eeba743be1a2d3d0e1ca4e"
        "7beb2521f0790f2bf3789af6de1302a15a51d488bf2b4a282f4586f47fa1"
        "af1315c837"), Ctx2, 1, 0);

//5        open_cursor csr2
        lrd_open_cursor(&Csr2, Con2, 0);
       

        lrd_db_option(Csr2, OT_ODBC_CURSOR_UNBOUNDCOLS, 0, 0);
//6        db set. If you open “sql query”, query will set default.Sometimes it will influence the test result.  
        lrd_stmt(Csr2, "set nocount off\r\nset arithabort off\r\nset "
        "concat_null_yields_null off\r\nset ansi_nulls off\r\nset "
        "cursor_close_on_commit off\r\nset ansi_null_dflt_on off\r\n"
        "set implicit_transactions off\r\nset ansi_padding off\r\n"
        "set ansi_warnings off\r\nset quoted_identifier off\r\n", -1, 1 /*Direct exec*/, 0 /*None*/, 0);

/*all above script is to open and connect db.
next script is operation */       

        lrd_cancel(0, Csr2, 0 /*Unused*/, 0);
//7        DB: pubs
        lrd_stmt(Csr2, "use pubs\r\n", -1, 1 /*Direct exec*/, 0 /*None*/, 0);
        lrd_db_option(Csr2, OT_ODBC_CURSOR_UNBOUNDCOLS, 0, 0);
        lrd_result_set(Csr2, 0, 0, 0);
        lrd_cancel(0, Csr2, 0 /*Unused*/, 0);
        lrd_cancel(0, Csr2, 0 /*Unused*/, 0);
        lrd_cancel(0, Csr2, 0 /*Unused*/, 0);
        lrd_cancel(0, Csr2, 0 /*Unused*/, 0);

/*select query
        You can get the transaction "query" time as the  sql execute time.And you can get the transaction
        "getData" time as the  sql data transmission time between db and aplication server.*/

        lr_start_transaction("query");

        lrd_stmt(Csr2, "select * from dbo.authors\r\n", -1, 1 /*Direct exec*/, 0 /*None*/, 0);
        lrd_bind_cols(Csr2, BCInfo_D37, 0);
        lr_end_transaction("query", LR_AUTO);

        lr_start_transaction("getData");

        lrd_fetch(Csr2, -23, 1, 0, PrintRow18, 0);
        GRID(18);
        lr_end_transaction("getData", LR_AUTO);

        lrd_db_option(Csr2, OT_ODBC_CURSOR_UNBOUNDCOLS, 0, 0);
        lrd_result_set(Csr2, 0, 0, 0);
        lrd_cancel(0, Csr2, 0 /*Unused*/, 0);
        lrd_cancel(0, Csr2, 0 /*Unused*/, 0);
        lrd_cancel(0, Csr2, 0 /*Unused*/, 0);
        lrd_cancel(0, Csr2, 0 /*Unused*/, 0);

//        exec procedure
        lrd_stmt(Csr2, "exec dbo.byroyalty 100\r\n", -1, 1 /*Direct exec*/, 0 /*None*/, 0);
        lrd_bind_cols(Csr2, BCInfo_D39, 0);


        lrd_fetch(Csr2, -10, 1, 0, PrintRow20, 0);
        GRID(20);
       

        lrd_db_option(Csr2, OT_ODBC_CURSOR_UNBOUNDCOLS, 0, 0);
        lrd_result_set(Csr2, 0, 0, 0);
        lrd_cancel(0, Csr2, 0 /*Unused*/, 0);
        lrd_cancel(0, Csr2, 0 /*Unused*/, 0);
        lrd_cancel(0, Csr2, 0 /*Unused*/, 0);
        lrd_cancel(0, Csr2, 0 /*Unused*/, 0);
        lrd_result_set(Csr2, 0, 0, 0);
        lrd_db_option(Csr2, OT_ODBC_CURSOR_CLOSE, 0, 0);
        lrd_db_option(Csr2, OT_ODBC_CURSOR_UNBOUNDCOLS, 0, 0);
       
// script end . close cusor、connectio、context       
        lrd_close_cursor(&Csr2, 0);
        lrd_close_connection(&Con2, 0, 0);
        lrd_free_connection(&Con2, 0 /*Unused*/, 0);
        lrd_close_context(&Ctx2, 0, 0);
       
        return 0;
作者: fish_yy    时间: 2005-12-12 13:35
请问该种情况应该选中那种协议,我如果仅选择了web/http协议,提示说vuser_init.c (9): undeclared identifier `InitInfo'
vuser_init.c (9): type error in argument 1 to `lrdfnc_init'; found `pointer to int' expected `const pointer to struct LRD_INIT_INFO'
vuser_init.c (9): undeclared identifier `DBTypeVersion'等等,请帮忙解答一下




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2