DbHandle.bas
上传用户:cntx88
上传日期:2022-08-07
资源大小:169k
文件大小:2k
- Attribute VB_Name = "DbHandle"
- Option Explicit
- Public DbFinance As New ADODB.Connection '设置ADODB连接对象的公共变量
- Public Function DbConnection() '打开数据库
- Dim e As ADODB.Error
-
- DbConnection = True 'ConnectDB为真时表示打开数据库无误
-
- On Error GoTo errorhandle '出现任何错误将执行errorhandle进行错误处理
- DbFinance.Errors.Clear
- DbFinance.CommandTimeout = 30 '执行SQL命令的缺省等待时间
- DbFinance.ConnectionTimeout = 30 '连接数据库的缺省时间
- DbFinance.ConnectionString = "DSN=Finance" '打开DSN名为Finance的odbc数据源
-
- On Error GoTo Unconnect '出现数据库连接错误时将进行处理
- DbFinance.Open '打开数据库
- Exit Function
-
- errorhandle:
- If DbFinance.Errors.Count < 1 Then
- MsgBox Err.Description, vbOKOnly + vbExclamation '显示错误描述
- Else
- For Each e In DbFinance.Errors
- MsgBox e.Description '显示错误描述
- Next e
- End If
- DbConnection = False 'DbConnection为假(False)时表示打开数据库有误
- Set DbFinance = Nothing '关闭错误的数据库
- Exit Function
- Unconnect:
- MsgBox "数据库网络连接失败,程序将终止!", vbCritical + vbOKOnly '显示错误描述
- Set DbFinance = Nothing '关闭错误的数据库
- DbConnection = False
- Exit Function
- End Function
- Public Sub DbClose() '关闭数据库
- On Error Resume Next
- DbFinance.Close
- Set DbFinance = Nothing
- End Sub
- Public Function resultcount(rs As ADODB.Recordset) As Long '返回结果集中的记录数
- On Error Resume Next
- resultcount = 0
- Do While rs.EOF = False
- resultcount = resultcount + 1
- rs.MoveNext
- Loop
- rs.MoveFirst
- End Function