首页 > 开发 > 综合 > 正文

用api实现数据库的操作!

2024-07-21 02:23:25
字体:
来源:转载
供稿:网友
  • 网站运营seo文章大全
  • 提供全面的站长运营经验及seo技术!
  • 其实操作数据库有很多种方式,比如dao、ado等。不过这些方式在发布的时候都需要带上一些运行库,少则几兆,多则十几兆。本来很简单的对数据库的操作,发布的时候带上这些库之后,程序有十几兆。笔者在实践中,总结了用api进行操作数据库的方法,对于一些简单的数据库操作还是可以用这种方法实现的。最大的优点就是可以省去运行库的支持。大大的简小安装包的尺寸。



                             崔占民

                         email:[email protected]



    首先添加一个模块,方法:菜单->工程->添加模块,代码如下:



    option explicit

    declare function sqlallocenv lib "odbc32.dll" (phenv&) as integer
    declare function sqlallocconnect lib "odbc32.dll" (byval henv&, phdbc&) as integer
    declare function sqlallocstmt lib "odbc32.dll" (byval hdbc&, phstmt&) as integer
    declare function sqlconnect lib "odbc32.dll" (byval hdbc&, byval szdsn$, byval cbdsn%, byval szuid$, byval cbuid%, byval szauthstr$, byval cbauthstr%) as integer
    declare function sqlcolattributesstring lib "odbc32.dll" alias "sqlcolattributes" (byval hstmt&, byval icol%, byval fdesctype%, byval rgbdesc as string, byval cbdescmax%, pcbdesc%, pfdesc&) as integer
    declare function sqldisconnect lib "odbc32.dll" (byval hdbc&) as integer
    declare function sqlexecdirect lib "odbc32.dll" (byval hstmt&, byval szsqlstr$, byval cbsqlstr&) as integer
    declare function sqlfetch lib "odbc32.dll" (byval hstmt&) as integer
    declare function sqlfreeconnect lib "odbc32.dll" (byval hdbc&) as integer
    declare function sqlfreeenv lib "odbc32.dll" (byval henv&) as integer
    declare function sqlfreestmt lib "odbc32.dll" (byval hstmt&, byval foption%) as integer
    declare function sqlgetdata lib "odbc32.dll" (byval hstmt&, byval icol%, byval fctype%, byval rgbvalue as string, byval cbvaluemax&, pcbvalue&) as integer
    declare function sqlsetdata lib "odbc32.dll" (byval hstmt&, byval icol%, byval fctype%, byval rgbvalue as string, byval cbvaluemax&, pcbvalue&) as integer
    declare function sqlnumresultcols lib "odbc32.dll" (byval hstmt&, pccol%) as integer
    declare function sqlnumresultrols lib "odbc32.dll" (byval hstmt&, pcrol%) as long

    global const sql_c_char as long = 1
    global const sql_column_label as long = 18
    global const sql_drop as long = 1
    global const sql_error as long = -1
    global const sql_no_data_found as long = 100
    global const sql_success as long = 0

    public rc as long '注释:odbc函数的返回码
    public henv as long '注释:odbc环境句柄
    public hdbc as long


    添加一个msflexgrid控件,用来显示从数据库中查询出来的数据,代码如下:

    option explicit

    private sub command1_click()
    unload me
    end sub

    private sub form_load()
    rc = sqlallocenv(henv)
    if rc <> 0 then
    msgbox "无法初始化odbc"
    end
    end if

    rc = sqlallocconnect(henv, hdbc)
    if rc <> 0 then
    msgbox "无法获得连接句柄"
    rc = sqlfreeenv(henv)
    end
    end if

    dim dsn as string, uid as string, pwd as string

    dsn = "powersoft demo db v6"
    uid = "dba"
    pwd = "sql"
    rc = sqlconnect(hdbc, dsn, len(dsn), uid, len(uid), pwd, len(uid))

    if rc = sql_error then
    msgbox "无法建立与odbc数据源的连接"
    unload me
    end if
    end sub

    private sub cmdquery_click()
    on error resume next
    dim hstmt as long
    dim sqlstmt as string
    dim rscols as integer, rsrows as long
    dim i as integer, j as integer
    dim colval as string * 1024
    dim colvallen as long, collablen as integer, larg as long

    grid1.redraw = false

    rc = sqlallocstmt(hdbc, hstmt)
    if rc <> sql_success then
    msgbox "无法获得sql语句句柄"
    exit sub
    end if

    sqlstmt = "select * from exam_xref_info"
    rc = sqlexecdirect(hstmt, sqlstmt, len(sqlstmt))
    if rc <> sql_success then
    msgbox "sql语句执行失败"
    exit sub
    end if

    rc = sqlnumresultcols(hstmt, rscols)
    if rscols > 1 then
    grid1.cols = rscols
    grid1.rows = 10
    grid1.row = 0
    else
    exit sub
    end if

    for i = 1 to rscols
    rc = sqlcolattributesstring(hstmt, i, sql_column_label, colval, 255, collablen, larg)
    grid1.col = i
    grid1.text = left(colval, collablen)
    next i

    do until sqlfetch(hstmt) = sql_no_data_found
    colval = string$(1024, 0)

    if grid1.row + 1 >= grid1.rows then
    grid1.rows = grid1.rows + 1
    end if

    grid1.row = grid1.row + 1

    for i = 1 to rscols
    rc = sqlgetdata(hstmt, i, sql_c_char, colval, len(colval), colvallen)
    grid1.col = i
    grid1.text = left$(colval, colvallen)
    next i
    loop

    rc = sqlfreestmt(hstmt, sql_drop)
    grid1.redraw = true
    end sub

    private sub form_queryunload(cancel as integer, unloadmode as integer)
    dim rc as integer

    if hdbc <> 0 then
    rc = sqldisconnect(hdbc)
    end if

    rc = sqlfreeconnect(hdbc)
    if henv <> 0 then
    rc = sqlfreeenv(henv)
    end if
    end sub


      实现的时候,将程序中的odbc名称及用户名与密码改成相应的就可以了。

    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表