怎么让VB与SQL2005相连?
1个回答
2010-02-11
展开全部
VB连接SQL数据库的模块:
Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public addFlag As Boolean
Public Function OpenCn() 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 = "127.0.0.1" ''服务器的名字
conn.Properties("initial catalog").Value = "chart" ''库名
conn.Properties("integrated security").Value = "SSPI" ''登陆袭兆类型
conn.Open
OpenCn = True
addFlag = True
Exit Function
strerrmag:
mag = "Data can''t connect"
Call MsgBox(mag, vbOKCancel, "Error:Data connect"汪前)
addFlag = False
End
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
Exit Function
strerrmag:
mag = "data not connect"
Call MsgBox(mag, vbOKCancel, "error:connect")
openrs = False
End
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() 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 = "127.0.0.1" ''服务器的名字
conn.Properties("initial catalog").Value = "chart" ''库名
conn.Properties("integrated security").Value = "SSPI" ''登陆袭兆类型
conn.Open
OpenCn = True
addFlag = True
Exit Function
strerrmag:
mag = "Data can''t connect"
Call MsgBox(mag, vbOKCancel, "Error:Data connect"汪前)
addFlag = False
End
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
Exit Function
strerrmag:
mag = "data not connect"
Call MsgBox(mag, vbOKCancel, "error:connect")
openrs = False
End
End Function
Public Sub clors()
On Error Resume Next
If rs.State <> adStateClosed Then rs.Clone
Set rs = Nothing
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询