1.init_conn() 初始化conection 模块:Public Function Connect() As Boolean '连接数据库 On Error GoTo err Dim strConn As String '如果连接标记为真,则返回。否则会出错 If Connect = True Then Exit Function
Exit Functionerr: Connect = False MsgBox err.Description, vbExclamation + vbOKOnlyEnd FunctionPublic Function Disconnect() As Boolean '断开与数据库的连接 On Error GoTo err '如果连接标记为假,标明已经断开连接,则直接返回 If Connect = False Then Exit Function End If
'断开连接 g_Cnn.Close Set g_Cnn = Nothing
Disconnect = True
Exit Functionerr: MsgBox err.Description Exit Function
End Function________________________________________________2.get_rs()执行一条sql 返回rs模块: Public Function get_rs()(ByVal rs As ADODB.Recordset, _ ByVal sql As String) As Boolean '打开某记录集 On Error GoTo err
get_rs() = False
if Connect=false then exit function Set rs = New ADODB.Recordset With rs .CursorLocation = adUseClient .CursorType = adOpenStatic .LockType = adLockReadOnly .Open sql, g_Cnn, , , adCmdText End With get_rs() = TrueExit Functionerr: Exit FunctionEnd Function注:通常会把以上内容都做成一个DLL,在取得RS之后把RS的值通过数组或者XML形式回传给用户端,并CLOSE RS/SET RS=NOTHING,最后还要Disconnect,释放资源.3.exec_sql()执行一条sqlPublic Function exec_sql(ByVal strSQL As String) As Boolean On Error GoTo err Dim rs As ADODB.Recordset
exec_sql=false If Connect = False Then Exit Function g_Cnn.Execute
exec_sql=true
Disconnect
Exit Functionerr: MsgBox err.Description Exit Function
get_rs()执行一条sql 返回rs
exec_sql()执行一条sql
模块:Public Function Connect() As Boolean '连接数据库
On Error GoTo err
Dim strConn As String '如果连接标记为真,则返回。否则会出错
If Connect = True Then Exit Function
Set g_Cnn = New ADODB.Connection
strConn = "Driver={SQL Server};Server=" & g_ServerName & _
";Uid=zm;Database=" & g_DataName
g_Cnn.CursorLocation = adUseClient
g_Cnn.Open strConn
'设置连接标记
Connect = True
Exit Functionerr:
Connect = False
MsgBox err.Description, vbExclamation + vbOKOnlyEnd FunctionPublic Function Disconnect() As Boolean '断开与数据库的连接
On Error GoTo err '如果连接标记为假,标明已经断开连接,则直接返回
If Connect = False Then
Exit Function
End If
'断开连接
g_Cnn.Close
Set g_Cnn = Nothing
Disconnect = True
Exit Functionerr:
MsgBox err.Description
Exit Function
End Function________________________________________________2.get_rs()执行一条sql 返回rs模块:
Public Function get_rs()(ByVal rs As ADODB.Recordset, _
ByVal sql As String) As Boolean '打开某记录集
On Error GoTo err
get_rs() = False
if Connect=false then exit function Set rs = New ADODB.Recordset
With rs
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Open sql, g_Cnn, , , adCmdText
End With get_rs() = TrueExit Functionerr:
Exit FunctionEnd Function注:通常会把以上内容都做成一个DLL,在取得RS之后把RS的值通过数组或者XML形式回传给用户端,并CLOSE RS/SET RS=NOTHING,最后还要Disconnect,释放资源.3.exec_sql()执行一条sqlPublic Function exec_sql(ByVal strSQL As String) As Boolean
On Error GoTo err Dim rs As ADODB.Recordset
exec_sql=false If Connect = False Then Exit Function g_Cnn.Execute
exec_sql=true
Disconnect
Exit Functionerr:
MsgBox err.Description
Exit Function
End Function