使用ado控件可以方便的在vb6中訪問(wèn)odbc數(shù)據(jù)庫(kù),但是通過(guò)直接放置adods控件來(lái)獲得ado的數(shù)據(jù)連接比較麻煩,我們可以在vb工程中創(chuàng)建一個(gè)公共數(shù)據(jù)模塊,將ado控件的初始化、建立連接、關(guān)閉連接等操作都寫(xiě)到函數(shù)中,這樣就可以在工程的其他模塊中共享調(diào)用這個(gè)ado連接。
一次完整的ado調(diào)用操作分為如下幾個(gè)步驟:
需要注意的是,每個(gè)動(dòng)態(tài)創(chuàng)建的ado同時(shí)只能被一個(gè)過(guò)程調(diào)用,如果需要進(jìn)行多表并行操作,可能需要在公共數(shù)據(jù)模塊中建立多個(gè)動(dòng)態(tài)ado。
下面是相關(guān)的代碼:
'-----------------------------------------------------------------
'如下代碼保存在名為my.bas的工程模塊中
public conn as adodb.connection '定義ado connection對(duì)象
public rs as adodb.recordset '定義ado recordset對(duì)象
'****************************
'打開(kāi)數(shù)據(jù)庫(kù)連接
'****************************
function connopen()
dim astr as string
set conn = new adodb.connection
astr = getdatabasepath 'mdb文件數(shù)據(jù)庫(kù)路徑
conn.connectionstring = "provider=microsoft.jet.oledb.4.0;data source=" & astr & ";persist security info=false"
'本例的odbc連接為jet4.0的直接到mdb文件的連接,如果使用odbc數(shù)據(jù)源可以使用如下connection串:
'provider=msdasql.1;password="";persist security info=true;data source=數(shù)據(jù)源名稱;initial catalog=數(shù)據(jù)表庫(kù)名稱
conn.open
set rs = new adodb.recordset
with rs do
activeconnection = conn
cursortype = adopendynamic
locktype = adlockoptimistic
end with
end function
'****************************
'關(guān)閉數(shù)據(jù)庫(kù)
'****************************
function connclose()
set rs = nothing
conn.close
set conn = nothing
end function
'**********************************************************
' 獲得數(shù)據(jù)庫(kù)路徑
' 本例數(shù)據(jù)庫(kù)保存在程序目錄下的dbs子目錄中,名為db1.mdb
'**********************************************************
public function getdatabasepath() as string
dim spath as string
if right$(app.path, 1) = "/" then
spath = app.path + "dbs/"
else
spath = app.path + "/dbs/"
end if
getdatabasepath = spath + "db1.mdb"
end function
'end of my.bas
'-----------------------------------------------------------------
如下示例代碼為my.bas的使用方法:
'-----------------------------------------------------------------
'使用rs對(duì)象執(zhí)行select語(yǔ)句
'tablename和fieldname分別為表名和字段名
'查詢結(jié)果保存在數(shù)組s中
private sub runselectcommand()
dim s(99) as string
dim i as integer
i=0
call my.connopen
my.rs.open "select * from tablename"
while not rs.eof
i=i+1
if not isnull(my.rs!fieldname) then s(i)=cstr(my.rs!fieldname)
rs.movenext
wend
call my.connclose
end sub
'使用conn對(duì)象執(zhí)行update/delete/insert語(yǔ)句
'sql語(yǔ)句放在變量ssql中
private sub runsqlcommand()
dim ssql as string
call my.connopen
my.conn.execute ssql
call my.connclose
end sub
'對(duì)于datagrid和datareport這些需要datasource的控件可以做如下操作
'使用select語(yǔ)句打開(kāi)rs的數(shù)據(jù)集
set obj.datasource=my.rs
'---------------------------------------------------------------------
這個(gè)方法對(duì)于開(kāi)發(fā)簡(jiǎn)單小型的mis系統(tǒng)很實(shí)用,也可以在報(bào)表和數(shù)據(jù)表中使用,缺點(diǎn)是在多表操作和函數(shù)嵌套調(diào)用時(shí),一個(gè)動(dòng)態(tài)ado對(duì)象不能同時(shí)執(zhí)行兩個(gè)工作,后一個(gè)寫(xiě)入的sql語(yǔ)句會(huì)覆蓋先寫(xiě)入的sql語(yǔ)句,當(dāng)在回到前一個(gè)過(guò)程時(shí),會(huì)因?yàn)樽侄握也坏蕉鲥e(cuò)。因此如果可能需要進(jìn)行多表操作,可以嘗試多定義幾個(gè)conn 和rs對(duì)象。
新聞熱點(diǎn)
疑難解答
圖片精選