怎么让VB与SQL2005相连?

 我来答
匿名用户
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
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式