博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

连接数据库返回recordset

Posted on 2008-10-21 17:54  佟艿芙  阅读(1057)  评论(0编辑  收藏  举报

Public Function ufmRecordSet(ByVal bstrSQL As String) As Recordset
    Dim pcnnCn As New ADODB.Connection
    Dim precRS As New ADODB.Recordset
   
    If mstrConnect = "" Then
        MsgBox "连接字符串为空", vbOKOnly + 64, ""
        Exit Function
    End If
    If bstrSQL = "" Then
        MsgBox "SQL语句为空", vbOKOnly + 64, ""
        Exit Function
    End If
   
    pcnnCn.ConnectionTimeout = mintTimeout
    pcnnCn.CommandTimeout = mintTimeout
    pcnnCn.Open mstrConnect
   
    precRS.CursorLocation = adUseClient
    precRS.LockType = adLockOptimistic
    precRS.CursorType = adOpenStatic
    Set precRS.ActiveConnection = pcnnCn
    precRS.Open bstrSQL
    Set precRS.ActiveConnection = Nothing
    Set ufmRecordSet = precRS
    Set precRS = Nothing
    Set pcnnCn = Nothing
   
End Function

mstrConnect = "Provider=SQLOLEDB.1;Password=" & mstrSQLPassword & ";Persist Security " & _
    " Info=True;User ID=" & mstrSQLUser & ";Initial Catalog=" & mstrSQLDBName & _
    ";Data Source=" & mstrSQLServerName

Public Const mintTimeout = 800         '数据连接延时