VBScript - Return 数组中的记录集(SQL 类似函数)

VBScript - Return a Recordset in an Array (SQL Like function)

我必须为我公司的会计编写一个程序,但我在 return 将文章的家族放入数组中时遇到问题,我想要的所有家族都有一个以以下开头的会计代码“707”。这是我在 VBScript 中的代码:

Set objConnection = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\BASES\Base.mdb;Persist Security Info=False"
rs.CursorLocation = adUseClient

FamilleQuery = "Select Code from FamilleArticle Where CptVenteFrance Like '707%'"
rs.Open FamilleQuery, objConnection, adOpenStatic, adLockOptimistic

'rs.MoveFirst
'Do
    'ListeFamille(rs.AbsolutePosition) = rs("Code")
    'rs.MoveNext
'Loop until rs.EOF

'ListeFamilleString = rs.GetString(AdClipString, -1,"/","/"," ")
'ListeFamille = split(ListeFamilleString,"/")

'Set ListeFamille = rs.GetRows

'for i=0 to ubound(rs)
    'ListeFamille(i) = rs.Fields("Code").Value(i)
'next

rs.Close
objConnection.Close

作为评论,我对 return 数组中记录集的结果进行了所有尝试,但没有人没有成功。 有人可以说我哪里错了吗?

您不需要在 vbScript 中使用 Set 来将数组设置为记录集的行。您只在设置对象引用的地方使用 Set,这里不是这种情况。

ListeFamille = rs.GetRows 会将 ListeFamille 设置为二维数组,其中包含记录集的结果。

这是我自己的函数库之一的摘录:

oConnection.open sConnectionString
oRecordSet.open sSql, oConnection
If oRecordSet.RecordCount = 0 Then
    DataArray = ""
Else
    oRecordSet.MoveFirst
    DataArray = oRecordSet.GetRows
End If

试试这个

Option Explicit

'ADO Constants
Const adCmdText = 1
Const adParamInput = 1
Const adVarWChar = 202

'Would usually be passed in from somewhere
Dim value: value = "707%"

Dim cmd, rs, data
Dim conn: conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\BASES\Base.mdb;Persist Security Info=False"
Dim sql: sql = "Select Code from FamilleArticle Where CptVenteFrance Like ?"

Set cmd = Server.CreateObject("ADODB.Command")
With cmd
  .ActiveConnection = conn
  .CommandType = adCmdText
  .CommandText = sql
  Call .Parameters.Append(.CreateParameter("@value", adVarWChar, adParamInput, 50))      

  Set rs = .Execute(, Array(value))
  If Not rs.EOF Then data = rs.GetRows()
  Call rs.Close()
  Set rs = Nothing
End With
Set cmd = Nothing

Dim row, rows

If IsArray(data) Then
  'Test data (2d Array, 0 = column, 1 = row)
  Call WScript.Echo(data(0, 0))

  'Retrieving all rows
  rows = UBound(data, 2)
  For row = 0 To rows
    'First column from each row.
    Call WScript.Echo(data(0, row))
  Next
Else
  'No records returned
End If

有用的链接

  • Using Stored Procedure in Classical ASP .. execute and get results(说说返回数据是一个数组你可以遍历)