机房收费系统总结(数据连接和查询)

                                       机房收费系统总结(数据连接和查询)

  机房收费系统数据连接的过程是,首先ODBC驱动器和后台数据库服务器建立连接,然后用代码和ODBC建立连接,然后代码通过ODBC来对数据库服务器中的数据进行操作,连接代码如下:

Public Function ConnectString() As String
    '建立远程服务器数据连接
    ConnectString = "Provider=SQLOLEDB;Data Source=192.168.24.176;Initial CataLog=ComputerRoomCharge_sys;UID=sa;PWD=123456"
    'ConnectString = "FileDSN = ComputerCharge.dsn; UID = sa; PWD = 123456 " 这里是建立本地数据连接
End Function
Public Function ExecuteSQL(ByVal SQL As String) As ADODB.Recordset
    Dim ojbCnn As Connection
    Dim Objrst As Recordset
    Dim Arry() As String
    
    
    On Error GoTo executesql_Error
    
    Arry() = Split(SQL)
    Set objCnn = New Connection
    objCnn.Open ConnectString
    
    
    
    '判断是否不是查询语句
    If InStr("DELETE,INSERT,UPDATE", UCase(Arry(0))) Then
        objCnn.Execute SQL
        MsgBox "操作成功", vbOKOnly + vbExclamation, "提示"
    Else
        Set Objrst = New Recordset
        Objrst.Open Trim(SQL), objCnn, adOpenKeyset, adLockOptimistic
        Set ExecuteSQL = Objrst
        
    End If
    
'错误处理语句
executesql_exit:
    Set Objrst = Nothing
    Set objCnn = Nothing
    Exit Function
    
executesql_Error:
    MsgBox "数据库查询出现错误", vbOKOnly + vbExclamation, "提示"
    Resume executesql_exit


 

你可能感兴趣的:(机房收费系统总结(数据连接和查询))