宠辱不惊,看庭前花开花落;去留无意,望天空云卷云舒

VB 连接SQL的模块及form调用

上一篇 / 下一篇  2007-10-25 20:26:36

-------------------

'连接SQL的模块
Public conn As ADODB.Connection
Public Rs As ADODB.Recordset
Public addFlag As Boolean      '声明部分
Public Function OpenCn(ByVal Cip 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 = "navtech"          '库名
'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
conn.Open
OpenCn = True
If conn.State = 1 Then 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
     ōpenRs = True
     'End        '将记录集给rs
     Exit Function
strerrmag:
     mag = "data not connect"
     Call MsgBox(mag, vbOKOnly, "error:connect")
     ōpenRs = 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

-----------------------

from里的调用:

Private Sub Command1_Click()
a = Trim(Text1.Text)
b = Trim(Text2.Text)
c = Trim(Text3.Text)
Call OpenCn(a, b, c)
If addFlag = True Then MsgBox ("OK")
Call openRs("select * from command")
Set DataGrid1.DataSource = Rs
'rs.Close
End Sub


TAG:

 

评分:0

我来说两句

Open Toolbar