转至繁体中文版     | 网站首页 | 文章中心 | 下载中心 | 图片中心 | 笑话频道 | 教程频道 | 会员中心 | 雁过留声 | 
最新公告:     "MinTao学以致用网"欢迎您的光临,你的支持便是我们的动力,欢迎广大网友和各界人士亲临指导,你们的一个小小的建议便是我们发展的开路石!  [MinTao  2007年9月5日]        
您现在的位置: MinTao学以致用网 >> 文章中心 >> 电子课堂 >> 数据库 >> Sql Server >> 文章正文
专题栏目
更多内容
最新推荐 更多内容
相关文章
SQL Server的存储过程调
SQL Server安全性简介
SQL 语法参考手册
MS SQL Sever 7.0 存储引
SQL SERVER和SYBASE
SQL Server 7.0数据库的
VB应用程序访问SQL Serv
将DBF数据库转换成SQL S
拷贝的SQL Server 7数据
浅谈优化SQL Server数据
更多内容
SQL relay的C接口         
SQL relay的C接口
作者:playmud 文章来源:不详 点击数: 更新时间:2007-11-14 13:07:07
sqlrcon_endSession(con); for (row=0; row<sqlrcur_rowCount(cur); row++) { char **rowarray=sqlrcur_getRow(cur,row); for (col=0; col<sqlrcur_colCount(cur); col++) { printf("%s,",rowarray[col]); } printf("\n"); } sqlrcur_free(cur); sqlrcon_free(con); }

The sqlrcur_getFieldByIndex(), sqlrcur_getFieldByName() and sqlrcur_getRow() functions return NULL fields as empty strings. If you would like them to come back as NULL''''s instead, you can call the sqlrcur_getNullsAsNulls() method. To revert to the default behavior, you can call sqlrcur_getNullsAsEmptyStrings().

If you want to access the result set, but don''''t care about the column information (column names, types or sizes) and don''''t mind getting fields by their numeric index instead of by name, you can call the sqlrcur_dontGetColumnInfo() method prior to executing your query. This can result in a performance improvement, especially when many queries with small result sets are executed in rapid succession. You can call sqlrcur_getColumnInfo() again later to turn off this feature.

Dealing With Large Result Sets

SQL Relay normally buffers the entire result set. This can speed things up at the cost of memory. With large enough result sets, it makes sense to buffer the result set in chunks instead of all at once.

Use sqlrcur_setResultSetBufferSize() to set the number of rows to buffer at a time. Calls to sqlrcur_getRow(), sqlrcur_getFieldByIndex() and sqlrcur_getFieldByName() cause the chunk containing the requested field to be fetched. Rows in that chunk are accessible but rows before it are not.

For example, if you setResultSetBufferSize(5) and execute a query that returns 20 rows, rows 0-4 are available at once, then rows 5-9, then 10-14, then 15-19. When rows 5-9 are available, getFieldByIndex(0,0) will return NULL and getFieldByIndex(11,0) will cause rows 10-14 to be fetched and return the requested value.

When buffering the result set in chunks, don''''t end the session until after you''''re done with the result set.

If you call sqlrcur_setResultSetBufferSize() and forget what you set it to, you can always call sqlrcur_getResultSetBufferSize().

When buffering a result set in chunks, the sqlrcur_rowCount() method returns the number of rows returned so far. The sqlrcur_firstRowIndex() method returns the index of the first row of the currently buffered chunk.

#include <sqlrelay/sqlrclientwrapper.h>
#include <stdio.h>

main() {

        int     done=0;
        int     row=0;
        int     col;
        char    *field;

        sqlrcon      con=sqlrcon_alloc("host",9000,"","user","password",0,1);
        sqlrcur      cur=sqlrcur_alloc(con);

        sqlrcur_setResultSetBufferSize(cur,5);

        sqlrcur_sendQuery(cur,"select * from my_table");

        while (!done) {
                for (col=0; col<sqlrcur_colCount(cur); col++) {
                        if (field=sqlrcur_getFieldByIndex(cur,row,col)) {
                                printf("%s,",field);
                        } else {
                                done=1;
                        }
                }
                printf("\n");
                row++;
        }

        sqlrcur_sendQuery(cur,"select * from my_other_table");

        ... process this query''''s result set in chunks also ...

        sqlrcur_setResultSetBufferSize(cur,0);

        sqlrcur_sendQuery(cur,"select * from my_third_table");

        ... process this query''''s result set all at once ...

        sqlrcon_endSession(con);

        sqlrcur_free(cur);
        sqlrcon_free(con);
}
Cursors

Cursors make it possible to execute queries while processing the result set of another query. You can select rows from a table in one query, then iterate through it''''s result set, inserting rows into another table, using only 1 database connection for both operations.

For example:

#include <sqlrelay/sqlrclientwrapper.h>

main() {

        sqlrcon      con;
        sqlrcur      cursor1;
        sqlrcur      cursor2;
        int          index;

        con=new sqlrcon_alloc("host",9000,"","user","password",0,1);
        cursor1=new sqlrcur_alloc(con);
        cursor2=new sqlrcur_alloc(con);

        sqlrcur_setResultSetBufferSize(cursor1,10);
        sqlrcur_sendQuery(cursor1,"select * from my_huge_table");

        index=0;
        while (!sqlrcur_endOfResultSet(cursor1)) {
                sqlrcur_prepareQuery(cursor2,"insert into my_other_table values (:1,:2,:3)");
                sqlrcur_inputBindString(cursor2,"1",sqlrcur_getFieldByIndex(cursor1,index,1));
                sqlrcur_inputBindString(cursor2,"2",sqlrcur_getFieldByIndex(cursor1,index,2));
                sqlrcur_inputBindString(cursor2,"3",sqlrcur_getFieldByIndex(cursor1,index,3));
                sqlrcur_executeQuery(cursor2);
        }

        sqlrcur_free(cursor2);
        sqlrcur_free(cursor1);
        sqlrcon_free(con);
}

Prior to SQL Relay version 0.25, you would have had to buffer the first result set or use 2 database connections instead of just 1.

If you are using stored procedures with Oracle 8i or higher, a stored procedure can execute a query and return a cursor. A cursor bind variable can then retrieve that cursor. Your program can retrieve the result set from the cursor. All of this can be accomplished using defineOutputBindCursor(), getOutputBindCursor() and fetchFromOutputBindCursor()

上一页  [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]  ... 下一页  >> 

文章录入:mintao    责任编辑:mintao 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007 MinTao学以致用网(www.mintao.net) Inc All Rights Reserved.
    QQ:543098146有事请Q我! QQ:261561092有事请Q我 QQ:179647303有事请Q我 MSN:min906@126.com
    站长:MinTao 信息产业部ICP备案号:鄂ICP备07500065号

    学以致用是我们学习者的至高境界和不懈追求,[MinTao学以致用网]与大家共同学习,共同进步……
    信息产业部备案
    *鄂ICP备07500065号