前端之家收集整理的这篇文章主要介绍了
VB连接SQL数据库驱动部分,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Attribute VB_Name = "Data_
sql" Public conn As ADODB.Connection Public rs As ADODB.Recordset 'Dim proc As New ADODB.Command Public addFlag As Boolean '声明部分 Public Function OpenCn(ByVal Cip As String,ByVal DBName As String,ByVal users As String,ByVal pw 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 = DBName '库名 'conn.Properties("integrated security").Value = "SSPI" '登陆类型 conn.Properties("user id").Value = users '
sql库名 conn.Properties("password").Value = pw '密码 '
sql = "Provider=
sqlOLEDB.1;Persist Security Info=False;User ID=sa;password=;Initial Catalog=pubs;Data Source=127.0.0.1" '如果不用这个模块也行可以,这一句便是常用的引擎。 'conn.ConnectionString =
sql Data Source=.;Initial Catalog=Andon;User ID=sa conn.Open OpenCn = True If conn.State = 1 Then addFlag = True Exit Function strerrmag: mag = "Data can't connect" Call Msg
Box(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 str
sql 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 str
sql End With addFlag = True openRs = True 'End '将记录集给rs Exit Function strerrmag: mag = "data not connect" Call Msg
Box(mag,"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
原文链接:https://www.f2er.com/vb/259113.html