我正在尝试修复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)))
任何想法都会非常感激......
答案 0 :(得分:14)
你几乎就在那里,问题是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>"
%>
答案 1 :(得分:1)
第一个代码块看起来正确。您确定要读取返回数组的第二维数据吗?这就是GetRow填充数组的方式。
arrProducts(0,0)=&gt; prod_id - 第1行 arrProducts(1,0)=&gt; prod_description - 第1行
arrProducts(0,1)=&gt; prod_id - 第2行 arrProducts(1,1)=&gt; prod_description - 第2行
等等。还
Dim arrProducts()
应该是
Dim arrProducts
http://www.asp101.com/samples/viewasp.asp?file=db_getrows.asp
答案 2 :(得分:1)
扩展amit_g说明:
<% OPTION EXPLICIT %>
<%
sub echo(x)
response.write x
end sub
dim conn : set conn = server.createobject("ADODB.CONNECTION")
conn.open("Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;Initial Catalog=tinker;uid=sa;pwd=devpassword")
dim sql : sql = "select '1a' as ColA , '1b' as ColB union all select '2a' , '2b' union all select '3a' , '3b' "
dim rs : set rs = conn.execute(SQL)
dim arr : arr = rs.GetRows()
dim cols : cols = ubound(arr,1)
dim rows : rows = ubound(arr,2)
dim x , y
echo "<table border='1' style='border-collapse:collapse;'>"
echo "<tr>"
echo "<td> </td>"
for x = 0 to cols
echo "<th>Col " & x & "</th>"
next
echo "</tr>"
for y = 0 to rows
echo "<tr>"
echo "<th>Row " & y & "</th>"
for x = 0 to cols
echo "<td>" & arr(x,y) & "</td>"
next
echo "</tr>"
next
echo "</table>"
%>
答案 3 :(得分:1)
我认为在ASP中工作的PHP程序员最令人困惑的事情是数组维度与您期望的顺序相反。
来自PHP我希望theArray(0,2)成为第一个记录,第三列。不。这是第三条记录的第一列。如果你想要一些类似于关联数组的东西,你需要研究创建&#34;字典&#34;