📄 oprdb.bas
字号:
Attribute VB_Name = "oprDb"
Public conn As Connection 'conn为连接
Public Sub conntection()
'On Error GoTo dbError
Set conn = CreateObject("adodb.connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=./data.mdb;Persist Security Info=False" '打开数据源
conn.CursorLocation = adUseClient
'dbError:
' MsgBox "连接数据库时出现错误!"
End Sub
Public Function query(strSql As String) As Recordset
'On Error GoTo dbError
Dim rs As Recordset
Set rs = conn.Execute(strSql)
Set query = rs
'dbError:
' MsgBox "查询数据时出现错误!"
End Function
Public Function isExist(table As String, value As String, filed As String) As Boolean
Dim rs As Recordset
Set rs = query("select * from " + table + " where " + filed + "='" + value + "'")
Dim n As Integer
n = getRows(rs)
rs.Close
If (n > 0) Then
isExist = True
Else
isExist = False
End If
End Function
Public Function getRows(rs As Recordset) As Integer
'On Error GoTo dbError
Dim i As Integer
i = 0
While (rs.EOF = False)
rs.MoveNext
i = i + 1
Wend
getRows = i
'dbError:
' MsgBox "计算结果集数目时出现错误!"
' getRows = -1
End Function
Public Function add(strSql As String)
On Error GoTo dbError
conn.BeginTrans
conn.Execute (strSql)
conn.CommitTrans
dbError:
conn.RollbackTrans
MsgBox "添加数据时出现错误!"
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -