Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public addFlag As Boolean
Public Function OpenCn(ByVal Cip As String) As Boolean
Dim mag As String
On Error GoTo strerrmag
Set conn = New ADODB.Connection
conn.ConnectionTimeout = 25
conn.Provider = "sqloledb"
conn.Properties("data source").Value = Cip '服务器的名字
conn.Properties("initial catalog").Value = "zqoa" '库名
'conn.Properties("integrated security").Value = "SSPI" '登陆类型
conn.Properties("user id").Value = "sa"
conn.Properties("password").Value = "sa"
conn.Open
OpenCn = True
addFlag = True
Exit Function
strerrmag:
mag = "Data can't connect"
Call MsgBox(mag, vbOKOnly, "Error:Data connect")
addFlag = False
Exit Function
End Function
Public Sub cloCn()
On Error Resume Next
If conn.State <> adStateClosed Then conn.Close
Set conn = Nothing
End Sub
Public Function openRs(ByVal strsql As String) As Boolean '连接数据库记录集
Dim mag As String
Dim rpy As Boolean
On Error GoTo strerrmag
Set rs = New ADODB.Recordset
If addFlag = False Then rpy = True
With rs
.ActiveConnection = conn
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open strsql
End With
addFlag = True
openRs = True
End
'Exit Function
strerrmag:
mag = "data not connect"
Call MsgBox(mag, vbOKOnly, "error:connect")
openRs = False
End
'Exit Function
End Function
Public Sub cloRs()
On Error Resume Next
If rs.State <> adStateClosed Then rs.Clone
Set rs = Nothing
End Sub
博客展示了使用VB语言实现数据库连接和记录集操作的代码。包含打开数据库连接、关闭连接、打开记录集和关闭记录集等功能,使用ADODB对象,设置数据库连接属性,处理连接异常。
2万+

被折叠的 条评论
为什么被折叠?



