国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

在VB6中動(dòng)態(tài)創(chuàng)建使用ADO控件訪問(wèn)數(shù)據(jù)庫(kù)

2024-07-21 02:20:28
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

使用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è)步驟:

  1. 打開(kāi)ado到數(shù)據(jù)庫(kù)的連接,初始化ado recordset集。
  2. 通過(guò)寫(xiě)入sql語(yǔ)句執(zhí)行查詢并返回查詢結(jié)果recordset集;或者可以通過(guò)寫(xiě)入sql語(yǔ)句執(zhí)行相應(yīng)數(shù)據(jù)庫(kù)操作。
  3. 釋放recordset集,關(guān)閉數(shù)據(jù)庫(kù)連接。

需要注意的是,每個(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ì)象。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 靖江市| 西藏| 河津市| 兴宁市| 上蔡县| 临沂市| 佛坪县| 泰来县| 建水县| 宁津县| 宁阳县| 博客| 宁河县| 宜宾市| 桑植县| 镇康县| 肥城市| 龙州县| 锦州市| 长岛县| 嘉义市| 清水县| 桂林市| 喀什市| 武穴市| 安康市| 罗甸县| 同仁县| 饶阳县| 邯郸县| 乌兰浩特市| 株洲市| 邵阳市| 沁源县| 叶城县| 阿拉善左旗| 灵川县| 舞阳县| 闸北区| 余姚市| 龙川县|