vb连接sql数据库
答案:2 悬赏:40 手机版
解决时间 2021-02-24 08:05
- 提问者网友:动次大次蹦擦擦
- 2021-02-23 17:08
vb连接sql数据库
最佳答案
- 五星知识达人网友:雾月
- 2021-02-23 17:17
VB连接SQL模块。直接拿去用吧。
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
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
全部回答
- 1楼网友:往事隔山水
- 2021-02-23 18:36
多方式
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯