Classic ASP 3.0 Create Array from a Recordset

后端 未结 4 1183
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 07:31

I\'m trying to fix an ASP Classic app and when I try to create an array from a Recordset Object. However I Can\'t get it to work correctly.

This code gives me a sin

相关标签:
4条回答
  • I think the biggest confusing thing for a PHP programmer working in ASP is that array dimensions are in the reverse order of what you're expecting.

    Coming from PHP I would expect theArray(0,2) to be the first record, third column. Nope. That's the first column of the third record. And if you want something resembling associative arrays you need to look into creating "dictionaries"

    0 讨论(0)
  • 2020-12-06 07:41

    The first code block looks correct. Are you sure that you are reading the data in second dimension of the returned array? That is how the GetRow populates the array.

    arrProducts(0, 0) => prod_id - row 1 arrProducts(1, 0) => prod_description - row 1

    arrProducts(0, 1) => prod_id - row 2 arrProducts(1, 1) => prod_description - row 2

    and so on. Also

    Dim arrProducts()

    should be

    Dim arrProducts

    http://www.asp101.com/samples/viewasp.asp?file=db_getrows.asp

    0 讨论(0)
  • 2020-12-06 07:54

    Your are almost there, the problem is that GetRows() returns a 2 dimensional array, and you need to tell Ubound what dimension do you want.

    Working code:

    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>"
    %>
    
    0 讨论(0)
  • 2020-12-06 07:55

    To expand on amit_g explanation:

    <% 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>&nbsp;</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>"
    
    %>
    
    0 讨论(0)
提交回复
热议问题