通过函数返回本地存储的记录集

问题描述

我在 VB6 中有一个功能性脚本,我需要将其迁移到 VBScript。作为代码重组的一部分,我决定对利用函数返回记录集感兴趣。想要处理与数据库的连接,以便事务关闭,因此将记录集写入内存对象对我来说很有意义。通过多次迭代试图确定我的语法到底有什么问题,我不确定哪个是最引人注目的。

Sub Main()
    clientRecordset(" Exec [dbo].[StoredProc] ")

End Sub

Private function clientRecordset(sqlQuery) 

    Const adOpenStatic = 3
    Const adLockOptimistic = 3
    Const adUseClient = 3
    
    Dim objConnection,objRecordset
    
    Set objConnection = CreateObject("ADODB.Connection")
    Set objRecordset = CreateObject("ADODB.Recordset")
    Set clientRecordset = CreateObject("ADODB.Recordset")
    objConnection.Open _
    "{connection properties}"
    
    
    clientRecordset = objConnection.Execute(sqlQuery) ' Error: operation is not allowed when object is closed
    clientRecordset = objRecordset.Open sqlQuery,objConnection,adOpenStatic,adLockOptimistic 'Error: Expected end of statement 
    objRecordset.close()
    objConnection.close()
    SET objRecordset = nothing
    Set objConnection = nothing
End Function

解决方法

多年来我一直在生产中使用这样的东西。一旦我检索到 RecordSet,我就会循环它并使用字段分隔符 (FDel) 和行分隔符 (RDel) 将其连接成一个字符串,然后根据需要将它们拆分为一个数组。

objRecordSet.Open(Query,objConnection,adOpenStatic,adLockOptimistic)
objRecordSet.MoveFirst
Dim LCount = 0
Dim RCount = objRecordSet.RecordCount
Dim FCount = objRecordSet.Fields.Count
Dim FDel = "|"
Dim RDel = "$"
Dim Rows = ""
Do Until LCount = RCount
    For i = 0 To (FCount - 1)
        Fields = ""
        Fields = objRecordSet.Fields.Item(i)
        Rows = Rows & Fields & FDel
        Next
        Rows = Rows & RDel
        objRecordSet.MoveNext
        LCount = LCount + 1
    Loop
    Output = Rows

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...