日期:2012-04-28  浏览次数:20410 次

加入这个模块后,在程序中任意地方调用:

打开ado数据库连接:Call CnnDB(mcnn, sCnn, True)
关闭ado数据库连接:Call DisCnn(mcnn)

这样,让数据库连接更加方便被连接和关闭

'%#******************************************
'%#*窗体名称:MdlDB
'%#*功能描述:加入这个模块后调用 Call CnnDB(gcnnDB, sCnn, True)
'%#*作者: 宁彦彬
'%#*修改日期:2002-12-03
'%#******************************************
'%#*Option Explicit

Public Sub CnnDB(ByRef mcnn As ADODB.Connection, sCnn as string, Optional blnClient As Boolean)
'*purpose: connect local sqlserver
'*NOTE: 在这里数据连接
On Error GoTo MYERR
If blnClient = True Then
mcnn.CursorLocation = adUseClient
Else
mcnn.CursorLocation = adUseServer
End If
mcnn.ConnectionString = sCnn '定义好的
mcnn.Open
MYEXIT:
Exit Sub
MYERR:
MsgBox "服务器没有运行" & vbCrLf & Err.Number & vbCrLf & Err.Description, vbCritical, "错误"
GoTo MYEXIT
End Sub


Public Sub DisCnn(ByRef mcnn As ADODB.Connection)
'*purpose: disconnect local sqlserver
'*NOTE: 在这里数据断开连接
On Error GoTo MYERR
mcnn.Close
Set mcnn = Nothing
MYEXIT:
Exit Sub
MYERR:
GoTo MYEXIT
End Sub


Function serverDate(ByRef mcnn As ADODB.Connection)
'*purpose: 取到服务器的系统时间
'*NOTE: 必须在mcnn初始化后再用
' MsgBox serverDate(mcnn)
On Error GoTo MYERR
Dim rst As New ADODB.Recordset
Set rst = mcnn.Execute("select getdate()")
serverDate = rst(0)
rst.Close
Set rst = Nothing
MYEXIT:
Exit Function
MYERR:
GoTo MYEXIT
End Function