VB 6.0中的数据连接模块
生活随笔
收集整理的這篇文章主要介紹了
VB 6.0中的数据连接模块
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??1'==?標記數據庫是否連接?==
??2Private?IsConnect?As?Boolean
??3'==?標記執行Connect()函數后,訪問數據庫的次數?==
??4Private?Connect_Num?As?Integer
??5Private?cnn?As?ADODB.Connection???'連接數據庫的Connection對象
??6Private?rs?As?ADODB.Recordset?????'保存結果集的Recordset對象
??7
??8'連接數據庫
??9Private?Sub?Connect()Sub?Connect()
?10??'如果連接標記為真,則返回。否則會出錯
?11??If?IsConnect?=?True?Then
?12????Exit?Sub
?13??End?If
?14??
?15??'關鍵New用于創建新對象cnn
?16??Set?cnn?=?New?ADODB.Connection
?17??'設置連接字符串ConnectionString屬性
?18??cnn.ConnectionString?=?connstr
?19??'打開到數據庫的連接
?20??cnn.Open
?21??'判斷連接的狀態
?22??If?cnn.State?<>?adStateOpen?Then
?23????'如果連接不成功,則顯示提示信息,退出程序
?24????MsgBox?"數據庫連接失敗"
?25????End
?26??End?If
?27??
?28??'設置連接標記,表示已經連接到數據庫
?29??IsConnect?=?True
?30End?Sub
?31
?32'斷開與數據庫的連接
?33Private?Sub?Disconnect()Sub?Disconnect()
?34??Dim?Rc?As?Long
?35??'如果連接標記為假,標明已經斷開連接,則直接返回
?36??If?IsConnect?=?False?Then
?37????Exit?Sub
?38??
?39????
?40??End?If
?41??'關閉連接
?42??cnn.Close
?43??'釋放cnn
?44??Set?cnn?=?Nothing
?45??'設置連接標記,表示已經斷開與數據庫的連接
?46??IsConnect?=?False
?47End?Sub
?48
?49'使用Connect_Num控制數據庫連接
?50Public?Sub?DB_Connect()Sub?DB_Connect()
?51??Connect_Num?=?Connect_Num?+?1
?52??Connect
?53End?Sub
?54Public?Sub?DB_Disconnect()Sub?DB_Disconnect()
?55??If?Connect_Num?>=?CONNECT_LOOP_MAX?Then
?56????Connect_Num?=?0
?57????Disconnect
?58??End?If
?59End?Sub
?60'強制關閉api方式訪問的數據庫,計數器復位
?61Public?Sub?DBapi_Disconnect()Sub?DBapi_Disconnect()
?62??Connect_Num?=?0
?63??Disconnect
?64End?Sub
?65
?66'執行數據庫操作語句
?67Public?Sub?SQLExt()Sub?SQLExt(ByVal?TmpSQLstmt?As?String)
?68??'創建Command對象cmd
?69??Dim?cmd?As?New?ADODB.Command
?70??
?71??'連接到數據庫
?72??DB_Connect
?73??'設置cmd的ActiveConnection屬性,指定與其關聯的數據庫連接
?74??Set?cmd.ActiveConnection?=?cnn
?75??'設置要執行的命令文本
?76??cmd.CommandText?=?TmpSQLstmt
?77'?MsgBox?TmpSQLstmt
?78??'執行命令
?79???cmd.Execute
?80??'清空cmd對象
?81??Set?cmd?=?Nothing
?82??'斷開與數據庫的連接
?83??DB_Disconnect
?84End?Sub
?85
?86'執行數據庫查詢語句
?87Public?Function?QueryExt()Function?QueryExt(ByVal?TmpSQLstmt?As?String)?As?ADODB.Recordset
?88??'創建Recordset對象rst
?89??Dim?rst?As?New?ADODB.Recordset
?90??
?91??'連接到數據庫
?92??DB_Connect
?93??'設置rst的ActiveConnection屬性,指定與其關聯的數據庫連接
?94??Set?rst.ActiveConnection?=?cnn
?95??'設置游標類型
?96??rst.CursorType?=?adOpenDynamic
?97??'設置鎖定類型
?98??rst.LockType?=?adLockOptimistic
?99??'打開記錄集
100??rst.Open?TmpSQLstmt
101??'返回記錄集
102??Set?QueryExt?=?rst
103End?Function
104
??2Private?IsConnect?As?Boolean
??3'==?標記執行Connect()函數后,訪問數據庫的次數?==
??4Private?Connect_Num?As?Integer
??5Private?cnn?As?ADODB.Connection???'連接數據庫的Connection對象
??6Private?rs?As?ADODB.Recordset?????'保存結果集的Recordset對象
??7
??8'連接數據庫
??9Private?Sub?Connect()Sub?Connect()
?10??'如果連接標記為真,則返回。否則會出錯
?11??If?IsConnect?=?True?Then
?12????Exit?Sub
?13??End?If
?14??
?15??'關鍵New用于創建新對象cnn
?16??Set?cnn?=?New?ADODB.Connection
?17??'設置連接字符串ConnectionString屬性
?18??cnn.ConnectionString?=?connstr
?19??'打開到數據庫的連接
?20??cnn.Open
?21??'判斷連接的狀態
?22??If?cnn.State?<>?adStateOpen?Then
?23????'如果連接不成功,則顯示提示信息,退出程序
?24????MsgBox?"數據庫連接失敗"
?25????End
?26??End?If
?27??
?28??'設置連接標記,表示已經連接到數據庫
?29??IsConnect?=?True
?30End?Sub
?31
?32'斷開與數據庫的連接
?33Private?Sub?Disconnect()Sub?Disconnect()
?34??Dim?Rc?As?Long
?35??'如果連接標記為假,標明已經斷開連接,則直接返回
?36??If?IsConnect?=?False?Then
?37????Exit?Sub
?38??
?39????
?40??End?If
?41??'關閉連接
?42??cnn.Close
?43??'釋放cnn
?44??Set?cnn?=?Nothing
?45??'設置連接標記,表示已經斷開與數據庫的連接
?46??IsConnect?=?False
?47End?Sub
?48
?49'使用Connect_Num控制數據庫連接
?50Public?Sub?DB_Connect()Sub?DB_Connect()
?51??Connect_Num?=?Connect_Num?+?1
?52??Connect
?53End?Sub
?54Public?Sub?DB_Disconnect()Sub?DB_Disconnect()
?55??If?Connect_Num?>=?CONNECT_LOOP_MAX?Then
?56????Connect_Num?=?0
?57????Disconnect
?58??End?If
?59End?Sub
?60'強制關閉api方式訪問的數據庫,計數器復位
?61Public?Sub?DBapi_Disconnect()Sub?DBapi_Disconnect()
?62??Connect_Num?=?0
?63??Disconnect
?64End?Sub
?65
?66'執行數據庫操作語句
?67Public?Sub?SQLExt()Sub?SQLExt(ByVal?TmpSQLstmt?As?String)
?68??'創建Command對象cmd
?69??Dim?cmd?As?New?ADODB.Command
?70??
?71??'連接到數據庫
?72??DB_Connect
?73??'設置cmd的ActiveConnection屬性,指定與其關聯的數據庫連接
?74??Set?cmd.ActiveConnection?=?cnn
?75??'設置要執行的命令文本
?76??cmd.CommandText?=?TmpSQLstmt
?77'?MsgBox?TmpSQLstmt
?78??'執行命令
?79???cmd.Execute
?80??'清空cmd對象
?81??Set?cmd?=?Nothing
?82??'斷開與數據庫的連接
?83??DB_Disconnect
?84End?Sub
?85
?86'執行數據庫查詢語句
?87Public?Function?QueryExt()Function?QueryExt(ByVal?TmpSQLstmt?As?String)?As?ADODB.Recordset
?88??'創建Recordset對象rst
?89??Dim?rst?As?New?ADODB.Recordset
?90??
?91??'連接到數據庫
?92??DB_Connect
?93??'設置rst的ActiveConnection屬性,指定與其關聯的數據庫連接
?94??Set?rst.ActiveConnection?=?cnn
?95??'設置游標類型
?96??rst.CursorType?=?adOpenDynamic
?97??'設置鎖定類型
?98??rst.LockType?=?adLockOptimistic
?99??'打開記錄集
100??rst.Open?TmpSQLstmt
101??'返回記錄集
102??Set?QueryExt?=?rst
103End?Function
104
轉載于:https://www.cnblogs.com/ruinet/archive/2007/03/28/691471.html
總結
以上是生活随笔為你收集整理的VB 6.0中的数据连接模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Guice 1.0 用户指南
- 下一篇: 安装Tomcat6.0