?? clsdatabase.cls
字號:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsDatabase"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'*************************
'數(shù)據(jù)庫操作
'*************************
Option Explicit
Dim dbTemp As New ADODB.Connection
Public Sub 更新數(shù)據(jù)庫(strSql As String)
On Error GoTo ErrHandle:
Call 檢查連接
dbTemp.Execute (strSql)
Exit Sub
ErrHandle:
Call 重新連接
dbTemp.Execute (strSql)
End Sub
Public Sub 更新批量數(shù)據(jù)(strSql() As String)
On Error GoTo ErrHandle:
Call 檢查連接
Dim i As Integer
For i = 1 To UBound(strSql)
dbTemp.Execute (strSql(i))
Next
Exit Sub
ErrHandle:
Call 重新連接
For i = 1 To UBound(strSql)
dbTemp.Execute (strSql(i))
Next
End Sub
Public Function 取數(shù)據(jù)(strSql As String) As ADODB.Recordset
On Error GoTo ErrHandle:
Call 檢查連接
Set 取數(shù)據(jù) = dbTemp.Execute(strSql)
Exit Function
ErrHandle:
'很特殊的處理
If Err.Number = -2147217900 Then
MsgBox (Err.Description + " 存在錯誤輸入符號:單引號 ' ")
'不是妥當(dāng),騙過程序
Set 取數(shù)據(jù) = dbTemp.Execute("select * from EIS where ID=10000")
Exit Function
End If
Call 重新連接
Set 取數(shù)據(jù) = dbTemp.Execute(strSql)
End Function
Public Function 取字段數(shù)據(jù)(strSql As String) As Variant
On Error GoTo ErrHandle:
Call 檢查連接
Dim rsTemp As New ADODB.Recordset
Set rsTemp = dbTemp.Execute(strSql)
If rsTemp.EOF = False Then
rsTemp.MoveFirst
取字段數(shù)據(jù) = rsTemp.Fields(0).Value
Else
取字段數(shù)據(jù) = Null
End If
rsTemp.Close
Set rsTemp = Nothing
Exit Function
ErrHandle:
Call 重新連接
Set rsTemp = dbTemp.Execute(strSql)
If rsTemp.EOF = False Then
rsTemp.MoveFirst
取字段數(shù)據(jù) = rsTemp.Fields(0).Value
Else
取字段數(shù)據(jù) = Null
End If
rsTemp.Close
Set rsTemp = Nothing
End Function
Public Sub Class_Initialize()
If (dbTemp Is Nothing) = False Then
If dbTemp.State <> adStateClosed Then
dbTemp.Close
End If
End If
Call GetDataConnectionString
Call dbTemp.Open(g_szSQLConnStr)
If dbTemp.State = adStateClosed Then
MsgBox ("數(shù)據(jù)庫連接出錯!")
Call 退出系統(tǒng)
End If
End Sub
Public Sub Class_Terminate()
On Error GoTo ErrHandle:
If Not (dbTemp Is Nothing) Then
If dbTemp.State = adStateOpen Then
dbTemp.Close
End If
End If
Set dbTemp = Nothing
Exit Sub
ErrHandle:
Set dbTemp = Nothing
End Sub
Private Sub 檢查連接()
If (dbTemp Is Nothing) = True Then
Class_Initialize
Exit Sub
End If
If dbTemp.State = adStateClosed Then
Class_Initialize
End If
End Sub
Private Sub 重新連接()
If (dbTemp Is Nothing) = False Then
If dbTemp.State <> adStateClosed Then
dbTemp.Close
End If
End If
Set dbTemp = Nothing
Call dbTemp.Open(g_szSQLConnStr)
If dbTemp.State = adStateClosed Then
MsgBox ("數(shù)據(jù)庫連接出錯!")
Call 退出系統(tǒng)
End If
End Sub
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -