vbscript – 经典ASP 3.0从Recordset创建数组

前端之家收集整理的这篇文章主要介绍了vbscript – 经典ASP 3.0从Recordset创建数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试修复ASP Classic应用程序,当我尝试从Recordset对象创建一个数组时.但是,我不能让它正常工作.

这段代码给了我一条记录(最后一条记录),但据我所知,它是正确的:

Dim Products
Dim Products_cmd
Dim Products_numRows

Set Products_cmd = Server.CreateObject ("ADODB.Command")
Products_cmd.ActiveConnection = Conn
Products_cmd.CommandText = "SELECT prod_id,prod_description FROM dbo.products ORDER BY prod_description ASC" 
Products_cmd.Prepared = true

Set Products = Products_cmd.Execute
Products_numRows = 0

Dim arrProducts()
arrProducts = Products.GetRows()

使用这段代码给我一个“下标超出范围:’UBound’

Dim Products
Dim Products_cmd
Dim Products_numRows

Set Products_cmd = Server.CreateObject ("ADODB.Command")
Products_cmd.ActiveConnection = Conn
Products_cmd.CommandText = "SELECT prod_id,prod_description FROM dbo.products ORDER BY prod_description ASC" 
Products_cmd.Prepared = true

Set Products = Products_cmd.Execute
Products_numRows = 0
Dim arrProducts()
Dim counter

For counter = 0 to Products.RecordCount - 1
    ReDim Preserve arrProducts(counter,2)
    arrProducts(counter,0) = Products.Fields.Item("prod_id").Value
    arrProducts(counter,1) = Products.Fields.Item("prod_description").Value
    Products.MoveNext
Next
Response.Write(Str(UBound(arrProducts)))

任何想法都会非常感激……

解决方法

你几乎就在那里,问题是GetRows()返回一个二维数组,你需要告诉 Ubound你想要什么尺寸.

工作代码

Dim Products
Dim Products_cmd
Dim Products_numRows

Set Products_cmd = Server.CreateObject ("ADODB.Command")
Products_cmd.ActiveConnection = Conn
Products_cmd.CommandText = "SELECT prod_id,prod_description FROM dbo.products ORDER BY prod_description ASC" 
Products_cmd.Prepared = true

Set Products = Products_cmd.Execute

Dim arrProducts
arrProducts = Products.GetRows()

dim i
response.write "<table>"
For i = 0 to ubound(arrProducts,2)
   response.write "<tr>"
   response.write("<td>" + trim(i+1))
   response.write("<td>" + trim(arrProducts(0,i)))
   response.write("<td>" + trim(arrProducts(1,i)))
next
response.write "</table>"
%>
原文链接:https://www.f2er.com/aspnet/248077.html

猜你在找的asp.Net相关文章