12
返回列表 发新帖
楼主: keaide

[笔记] ODBC API常用函数诠释

[复制链接]
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
11#
 楼主| 发表于 2006-5-26 23:24 | 只看该作者
int fdirection 方向标志 1(下一个),2(第一个),3(最后一个),4(前一个)
      ref string szdsn 数据源名称
integer cbdsnmax数据源名称的最大长度
ref integer pcbdsn数据源名称的实际长度
ref string szdescription 数据源描述名称
integer cbdescriptionmax 数据源描述字符串的最大长度
ref integer pcbdescription数据源描述字符串的实际长度
    返回值:integer ,成功时,返回0,失败返回值小于0。
函数功能:释放SQL语句句柄。
13、function integer SQLAllocConnect(long henv,ref long hdbc)library "odbc32.dll"
参数:long henv ODBC数据源环境句柄。      
ref long hdbc 保存ODBC连接句柄。
返回值:integer ,成功时,返回0,失败返回值小于0。
函数功能:获取ODBC连接句柄。
-:)上面总算将列出的几个ODBC API函数描述完,等有时间结合具体例子来说明如何利用ODBC API函数访问数据库了-:)

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
12#
 楼主| 发表于 2006-5-26 23:25 | 只看该作者
例程:
定义实例变量:
protected:
long henv//sql 环境句柄
long hstmt//sql语句句柄
long hdbc//sql 连接句柄
定义句柄外部函数引用:
function integer SQLAllocEnv(ref long phenv) library "odbc32.dll"
     function integer SQLFreeEnv(long henv) library "odbc32.dll"
     function integer SQLDataSources(long henv,int fdirection,ref string szdsn,&
integer cbdsnmax,ref integer pcbdsn,ref string szdescription,integer cbdescriptionmax,ref integer pcbdescription)library "odbc32.dll"
     function integer SQLAllocConnect(long henv,ref long hdbc)library "odbc32.dll"
     function integer SQLConnect (long hstmt,ref string szdsn,integer dsnlen,ref string
szuid,integer uidlen,ref string szpwd,integer pwdlen) library "odbc32.dll"
     function integer SQLDisconnect (long hdbc) library "odbc32.dll"
     function integer SQLAllocStmt(long hdbc,ref long hstmt)library "odbc32.dll"

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
13#
 楼主| 发表于 2006-5-26 23:25 | 只看该作者
function integer SQLTables(long hstmt,ref string sztablequalifier,integer tablequalifierlen,ref string szowner,integer owerlen,ref string szname,integer namelen,
ref string sztype,integer typelen)library "odbc32.dll"
     function integer SQLColumns(long hstmt,ref string sztablequalifier,integer tablequalifierlen,ref string szowner,integer owerlen,ref string szname,integer namelen,
ref string sztype,integer typelen)library "odbc32.dll"
     function integer SQLBindCol(long hstmt,integer colnum,integer datatype,ref string name,long maxlen,ref long actlen) library "odbc32.dll"
     function integer SQLFetch(long hstmt)library "odbc32.dll"
     function integer SQLError(long lenv,long hdbc,long hstmt,ref string sqlstate,ref long nativeerror,ref string errormsg,integer errormsgmax,ref integer errormsglen)library "odbc32.dll"
     function integer SQLFreeStmt(long hstmt,integer Options)library "odbc32.dll"

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
14#
 楼主| 发表于 2006-5-26 23:25 | 只看该作者
声明一个函数wf_sqlerror()
代码如下:
string ls_sqlstate,ls_errormsg
integer li_errormsgmax,li_ret
long nativeerror
ls_errormsg=space(255)
ls_sqlstate=space(255)
li_ret=sqlerror(henv,hdbc,hstmt,ls_sqlstate,li_nativeerror,ls_errormsg,255,li_errormsgmax)
messagebox("ODBC:"+ls_sqlstate,ls_errormsg)
return
窗口的open 事件,写入如下代码:
string ls_dsn,ls_description
integer li_direction,li_dsnmax,li_dsn_len
integer li_descriptionmax,li_description_len,li_retval
ls_dsn=space(255)
li_dsnmax=len(li_dsn)
ls_description=space(255)
li_descriptionmax=len(ls_description)
if sqlallocenv(henv)=-1 then
wf_sqlerror()
else
li_driection=1
do while sqldatasources(henv,li_direction,ls_dsn,li_dsnmax,li_dsn_len,ls_description,li_descriptionmax,li_description_len)=0
lb_datasources.additem(ls_dsn0
loop
end if

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
15#
 楼主| 发表于 2006-5-26 23:26 | 只看该作者
窗口的close事件中写入如下代码:
sqldisconnect(hdbc)
sqlfreeenv(henv)
在cb_connect按钮的cliked事件中写入如下的代码:
integer li_ret
string ls_dsn,ls_uid,ls_pwd
string ls_qualifer,ls_owner,ls_name,ls_type,ls_table
long ll_len
ls_dsn=lb_datasources.selecteditem()
ls_uid=sle_uid.text
ls_pwd=sle_pwd.text
li_ret=sqlallocconnect(henv,hdbc)
if li_ret<0 then
   wf_sqlerror()
else
   li_ret=sqlconnect(hdbc,ls_dsn,len(ls_dsn),ls_uid,len(ls_uid),ls_pwd,len(ls_pwd))

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
16#
 楼主| 发表于 2006-5-26 23:26 | 只看该作者
if li_ret<0 then
       wf_sqlerror()
   else
       li_ret=sqlallocstmt(hdbc,hstmt)
       if li_ret<0 then
            wf_sqlerror()
       else
           ls_type="’TABLE’,’VIEW’"
           if cbx_systemtables.checked then
                ls_type=ls_type+",’SYSTEM TABLE ’"
          end if
          li_ret=SQLTABLES(hstmt,ls_qualifier,len(ls_qualifier),ls_owner,len(ls_owner),ls_name,len(ls_name),ls_type,len(ls_type))
         if li_ret<0 then
               wf_sqlerror()
         else
              ls_table=space(255)
              ll_len=255
             sqlbincol(hstmt,3,1,ls_table,ll_len,ll_en)
             lb_tables.setredraw(false)
             do while sqlfetch(hstmt)=0  
                  lb_tables.additem(ls_table)
            loop
            lb_tables.setredraw(true)
            li_ret=sqlfreestmt(hstmt,0)
      end if
  end if
end if
end if
this.enabled=false
this.default=false
cbx_systemtables.enabled=false

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
17#
 楼主| 发表于 2006-5-26 23:26 | 只看该作者
在lb_datasources的selectionchanged事件中写入如下代码:
sle_uid.text=""
sle_pwd.text=""
sqldisconnect(hdbc)
cbx_systemtables.enabled=true
cb_connect.enabled=true
lb_columns.reset()
lb_tables.reset()
sle_uid.setfocus()
cb_connect.default=true
在lb_tables的selectionchanged事件中写入如下代码:
integer li_ret,li_len
string ls_qualifier,ls_owner,ls_name,ls_column,ls_colname,ls_type
long ll_maxlen
lb_columns.reset()
ls_qualifier=space(255)
ls_owner=space(255)
ls_column=space(255)
ls_colname=space(255)
ls_type=space(255)
ls_name=space(255)
ls_name=this.selecteditem()
li_ret=sqlcolumns(hstmt,ls_quanlifier,0,ls_owner,0,ls_name,len(ls_name),ls_column,0)
if li_ret<0 then
      wf_sqlerror()
else
    ls_colname=space(255)
    ls_type=space(255)
    ll_maxlen=255
    sqlbindcol(hstmt,4,1,ls_colname,ll_maxlen,ll_maxlen)
    sqlbindcol(hstmt,6,1,ls_type,ll_maxlen,ll_maxlen)
    lb_columns.setredraw(false)
    do while sqlfetch(hstmt)=0
         lb_columns.additem(ls_colname+"~t"+ls_type)
    loop
    lb_columns.setredraw(true)
    li_ret=sqlfreestmt(hstmt,0)
end if
在按钮cb_exit的clicked事件中写入以下代码:
    close(parent)
在应用对象的open事件中写入以下代码:
    open(w_odbc_data_sources)

使用道具 举报

回复
论坛徽章:
9
授权会员
日期:2005-10-30 17:05:33ITPUB元老
日期:2005-11-26 08:47:14会员2006贡献徽章
日期:2006-04-17 13:46:34ITPUB新首页上线纪念徽章
日期:2007-10-20 08:38:442009日食纪念
日期:2009-07-22 09:30:00生肖徽章2007版:牛
日期:2009-10-02 22:59:182010新春纪念徽章
日期:2010-01-04 08:33:082010年世界杯参赛球队:乌拉圭
日期:2010-01-05 08:51:302010新春纪念徽章
日期:2010-03-01 11:19:54
18#
发表于 2006-5-29 22:35 | 只看该作者
通过ODBC API与用ODBC连接数据库有什么不同,性能差多少?具体怎么用这些API.请LZ说说

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表