網(wǎng)站運(yùn)營(yíng)seo文章大全提供全面的站長(zhǎng)運(yùn)營(yíng)經(jīng)驗(yàn)及seo技術(shù)!其實(shí)操作數(shù)據(jù)庫(kù)有很多種方式,比如dao、ado等。不過(guò)這些方式在發(fā)布的時(shí)候都需要帶上一些運(yùn)行庫(kù),少則幾兆,多則十幾兆。本來(lái)很簡(jiǎn)單的對(duì)數(shù)據(jù)庫(kù)的操作,發(fā)布的時(shí)候帶上這些庫(kù)之后,程序有十幾兆。筆者在實(shí)踐中,總結(jié)了用api進(jìn)行操作數(shù)據(jù)庫(kù)的方法,對(duì)于一些簡(jiǎn)單的數(shù)據(jù)庫(kù)操作還是可以用這種方法實(shí)現(xiàn)的。最大的優(yōu)點(diǎn)就是可以省去運(yùn)行庫(kù)的支持。大大的簡(jiǎn)小安裝包的尺寸。
崔占民
email:[email protected]
首先添加一個(gè)模塊,方法:菜單->工程->添加模塊,代碼如下:
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函數(shù)的返回碼
public henv as long '注釋:odbc環(huán)境句柄
public hdbc as long
添加一個(gè)msflexgrid控件,用來(lái)顯示從數(shù)據(jù)庫(kù)中查詢出來(lái)的數(shù)據(jù),代碼如下:
option explicit
private sub command1_click()
unload me
end sub
private sub form_load()
rc = sqlallocenv(henv)
if rc <> 0 then
msgbox "無(wú)法初始化odbc"
end
end if
rc = sqlallocconnect(henv, hdbc)
if rc <> 0 then
msgbox "無(wú)法獲得連接句柄"
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 "無(wú)法建立與odbc數(shù)據(jù)源的連接"
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 "無(wú)法獲得sql語(yǔ)句句柄"
exit sub
end if
sqlstmt = "select * from exam_xref_info"
rc = sqlexecdirect(hstmt, sqlstmt, len(sqlstmt))
if rc <> sql_success then
msgbox "sql語(yǔ)句執(zhí)行失敗"
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
實(shí)現(xiàn)的時(shí)候,將程序中的odbc名稱及用戶名與密碼改成相應(yīng)的就可以了。