欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL SetConnectAttr函数定义及用法讲解

程序员文章站 2023-11-15 19:23:04
sqlsetconnectattr 函数定义: 用法类似于sqlsetenvattr,该函数是设置连接的各项属性用的 sqlreturnsqlsetconnectattr( sqlhdbcconne...

sqlsetconnectattr

函数定义:

用法类似于sqlsetenvattr,该函数是设置连接的各项属性用的

sqlreturnsqlsetconnectattr(

sqlhdbcconnectionhandle,

sqlintegerattribute,

sqlpointervalueptr,

sqlintegerstringlength);

参数详解:

connectionhandle:输入变量

连接的句柄

attribute:输入变量 valueptr:输入变量

attribute

valueptrcontents

sql_attr_access_mode
(odbc 1.0)

sql_mode_read_only 设置为只读连接,update类似的操作都是不能在他下属的stmt中执行的,只能支持查询语句。

sql_mode_read_write默认值,可读可写

sql_attr_async_enable
(odbc 3.0)

该连接下属的stmthandle所执行的语句可以异步处理

sql_async_enable_off = off 默认值关闭异步处理

sql_async_enable_on = on开启异步处理

sql_attr_auto_ipd
(odbc 3.0)

不好意思说实话没搞懂这个是啥,如果有知道的大佬麻烦留言一下,我加上去帮助到更多的人

a read-only sqluinteger value that specifies whether automatic population of the ipd after a call tosqlprepareis supported:

sql_true = automatic population of the ipd after a call tosqlprepareis supported by the driver.

sql_false = automatic population of the ipd after a call tosqlprepareis not supported by the driver. servers that do not support prepared statements will not be able to populate the ipd automatically.

if sql_true is returned for the sql_attr_auto_ipd connection attribute, the statement attribute sql_attr_enable_auto_ipd can be set to turn automatic population of the ipd on or off. if sql_attr_auto_ipd is sql_false, sql_attr_enable_auto_ipd cannot be set to sql_true. the default value of sql_attr_enable_auto_ipd is equal to the value of sql_attr_auto_ipd.

this connection attribute can be returned bysqlgetconnectattrbut cannot be set bysqlsetconnectattr.

sql_attr_autocommit
(odbc 1.0)

设置是否自动提交:

sql_autocommit_off关闭自动提交,可以手动commit或者rollback

sql_autocommit_on 自动提交开启,一但执行会自动commit

sql_attr_connection_dead

(odbc 3.5)

终止连接

sql_cd_true关闭连接

sql_cd_false保持连接

sql_attr_connection_timeout
(odbc 3.0)

除了查询与登录外的操作超时时长,单位为秒,整型。默认为0秒,意思不是执行sql语句立马超时而是不会超时,一直等待语句完成才向后执行

sql_attr_current_catalog
(odbc 2.0)

一个包含database名称的字符串指针。

sql_attr_login_timeout
(odbc 1.0)

设置登录超时时长,同sql_attr_connection_timeout

sql_attr_metadata_id
(odbc 3.0)

设定连接catalog函数参数

sql_true设置启用标识符id

if sql_false设置不启用标识符id

sql_attr_odbc_cursors
(odbc 2.0)

设置连接游标参数

sql_cur_use_if_needed 当必要时采用游标

sql_cur_use_odbc 使用obdc游标

sql_cur_use_driver使用驱动游标

sql_attr_packet_size
(odbc 2.0)

设置连接包大小,格式为(void*)(整型数字)

sql_attr_quiet_mode
(odbc 2.0)

设置对话框,当窗口句柄为空的时候,设置为不显示对话框

sql_attr_trace
(odbc 1.0)

设置连接是否启用跟踪

sql_opt_trace_off 默认,不跟踪

sql_opt_trace_on 跟踪会将跟踪记录写入root下的sql.log

sql_attr_tracefile
(odbc 1.0)

设置跟踪文件路径,参数为一个null-terminated 字符串。

sql_attr_translate_lib
(odbc 1.0)

设置连接转化字符集的动态库路径,参数为一个null-terminated 字符串

sql_attr_translate_option
(odbc 1.0)

设置连接转化字符集的参数,详情见translation dlls.

sql_attr_txn_isolation
(odbc 1.0)

设置连接事务隔离级别

详情见transaction isolation levels.

stringlength:输入变量

如果valueptr为字符串,填入字符串长度或者sql_nts.

如果是二进制缓存填入sql_len_binary_attr(length)的结果if valueptr is a pointer to a不是上述两个填入sql_is_pointer.

如果是边长的数组根据情况填入sql_is_integer或者sql_is_uinteger

返回值:

返回值有四种:sql_success, sql_success_with_info, sql_invalid_handle, or sql_error.

查看详细错误信息可调用sqlgetdiagrec函数(之后章节讲解)。

用法:

1.设定建立连接超时(设定5秒超时,连接正常)

rs = sqlsetconnectattr(hdbc,sql_attr_login_timeout,(void*)5,0);

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

2.设定建立数据库连接(只读)

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_access_mode,(void*)sql_mode_read_only,0);

3.设定连接异步属性(关闭异步)

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_async_enable,(void*)sql_async_enable_off,0);

4.设定连接自动提交属性(自动提交)

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_autocommit,(void*)sql_autocommit_on,0);

5.设定连接请求超时时间(设置 请求超时)

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_connection_timeout,(void*)3600,0);

6.设定连接当前库(设置 当前库)

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_current_catalog,(void*)"system",sql_nts);

7.设定连接catalog函数参数(设置启用标识符)

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_metadata_id,(void*)sql_false,0);

8.设定连接游标参数(设置启用游标据需要)

rs = sqlsetconnectattr(hdbc,sql_attr_odbc_cursors,(void*)sql_cur_use_if_needed,0);

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

9.设定连接游标参数(设置启用odbc游标)

rs = sqlsetconnectattr(hdbc,sql_attr_odbc_cursors,(void*)sql_cur_use_odbc,0);

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

10.设定连接游标参数(设置启用驱动游标)

rs = sqlsetconnectattr(hdbc,sql_attr_odbc_cursors,(void*)sql_cur_use_driver,0);

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

11.设定连接包大小(设置包大小)

rs = sqlsetconnectattr(hdbc,sql_attr_packet_size,(void*)2000,0);

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

12.设定连接对话框(设置空指针句柄)

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_quiet_mode,(void*)null,0);

13.设定连接包对话框(设置句柄)

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_quiet_mode,(void*)ht,0);

14.设定连接不启用跟踪(设置不启用)

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_trace,(void*)sql_opt_trace_off,0);

15.设定连接启用跟踪(设置启用)

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_trace,(void*)sql_opt_trace_on,0);

16.设定连接跟踪路径(设置路径)

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_tracefile,(void*)"path",sql_nts);

17.设置连接转化字符集的动态库路径

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_translate_lib,(void*)"path_dll_name",sql_nts);

18.设置连接转化字符集的参数

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_translate_option,(void*)1,0);

19.设置连接事务隔离级别(读未提交)

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_txn_isolation,(void*)1,0);

20.设置连接事务隔离级别(读已提交)

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_txn_isolation,(void*)2,0);

21.设置连接事务隔离级别(可重复读)

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_txn_isolation,(void*)4,0);

22.设置连接事务隔离级别(串行化)

rs_c = sqlconnect(hdbc,(uchar*)szdsn,sql_nts,(uchar*)szuid ,sql_nts,(uchar*)szauthstr,sql_nts);

rs = sqlsetconnectattr(hdbc,sql_attr_txn_isolation,(void*)8,0);