循环列名并将它们放入变量中。 ASP

发布于 2024-12-04 05:26:24 字数 264 浏览 1 评论 0原文

我需要能够循环列名并将列名及其值放入变量中。我的代码看起来像这样。

SQL = "select column1, column2 from table1"
set rs = conn.execute(SQL)

For each fld in rs.fields
    fld.name = rs(fld.name)
Next

rs.close

但这是行不通的。如何循环遍历列名称并将它们设置为变量而不必指定每个列名称?

谢谢

I need to be able to loop through column names and put the coloumn name and it's value into a variable. My code looks like this.

SQL = "select column1, column2 from table1"
set rs = conn.execute(SQL)

For each fld in rs.fields
    fld.name = rs(fld.name)
Next

rs.close

But this is not working. How can I loop through the column names and set them as variables without having to specify each column name?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

第几種人 2024-12-11 05:26:24

我认为您应该使用 GetRows 方法。返回一个二维数组。您不需要通过 GetRows 获得列名称。
考虑一下:

SQL = "select column1, column2 from table1"
set rs = conn.execute(SQL)
arrRecs = rs.GetRows
For row = 0 To UBound(arrRecs, 2) 'Rows
    For col = 0 To UBound(arrRecs, 1) 'Columns
        Response.Write rs.Fields(col).Name & " = " & arrRecs(col, row) & " "
    Next
    Response.Write "<br />"
Next
rs.close

I think you should use GetRows method. Returns a two-dimensional array. You don't need column names through GetRows.
Consider this:

SQL = "select column1, column2 from table1"
set rs = conn.execute(SQL)
arrRecs = rs.GetRows
For row = 0 To UBound(arrRecs, 2) 'Rows
    For col = 0 To UBound(arrRecs, 1) 'Columns
        Response.Write rs.Fields(col).Name & " = " & arrRecs(col, row) & " "
    Next
    Response.Write "<br />"
Next
rs.close
冷默言语 2024-12-11 05:26:24

如果要存储字段名称,可以使用字典并将每个字段名称作为键,然后将每列的值作为数组。

首先,声明字典:

Set oData = Server.CreateObject("Scripting.Dictionary")

现在使用这两个循环填充数据:

For Each fld In rs.Fields
    oData.Add fld.Name, Array()
Next

Do Until rs.EOF
    For Each fld In rs.Fields
        tempArray = oData(fld.Name)
        ReDim Preserve tempArray(UBound(tempArray) + 1)
        tempArray(UBound(tempArray)) = rs(fld.Name)
        oData(fld.Name) = tempArray
    Next
    rs.MoveNext
Loop

最后,您可以使用以下代码显示所有列名称:

Response.Write("Column names:<br />")
For Each fld In oData.Keys
    Response.Write(fld & "   ")
Next
Response.Write("<br />")

或者显示特定列的值:

Response.Write("Values for column named Id:<br />")
If oData.Exists("Id") Then
    tempArray = oData("Id")
    For x=0 To UBound(tempArray)
        Response.Write(tempArray(x) & "<br />")
    Next
Else  
    Response.Write("Such column does not exist")
End If

If you want to store the field names, you can use Dictionary and have each field name as a key, then the values of each column as array.

First, declate the dictionary:

Set oData = Server.CreateObject("Scripting.Dictionary")

Now populate it with data using those two loops:

For Each fld In rs.Fields
    oData.Add fld.Name, Array()
Next

Do Until rs.EOF
    For Each fld In rs.Fields
        tempArray = oData(fld.Name)
        ReDim Preserve tempArray(UBound(tempArray) + 1)
        tempArray(UBound(tempArray)) = rs(fld.Name)
        oData(fld.Name) = tempArray
    Next
    rs.MoveNext
Loop

And finally, you can show all the column names using such code:

Response.Write("Column names:<br />")
For Each fld In oData.Keys
    Response.Write(fld & "   ")
Next
Response.Write("<br />")

Or show the values for specific column:

Response.Write("Values for column named Id:<br />")
If oData.Exists("Id") Then
    tempArray = oData("Id")
    For x=0 To UBound(tempArray)
        Response.Write(tempArray(x) & "<br />")
    Next
Else  
    Response.Write("Such column does not exist")
End If
狼性发作 2024-12-11 05:26:24

使用上面的代码,并将名称/值对存储在会话中,例如 session("table." & fieldname) = rs(fieldname)。我经常用它来存储用户、客户、供应商等信息。也像缓存一样工作。您可以创建像 reloadCustomer(customerNumber) 这样的子例程来重新加载。这样你就不会去数据库获取基本信息。更改时更新它...

字典对象:

set dic = server.createobject("scripting.dictionary")
dic.add "keyName1", "Michael"
dic.add "keyName2", "John"
dic.add "keyName3", "Steve"

dic.Item("keyName2") <-- gives you "John"

Use the code above, and store the name/value pair in session, like session("table." & fieldname) = rs(fieldname). I use it a lot for storing user, customer, supplier, etc., information. Works like caching too. You can create sub routines like reloadCustomer(customerNumber) to reload. That way you aren't going to the DB for basic info. Update it when changed...

Dictionary object:

set dic = server.createobject("scripting.dictionary")
dic.add "keyName1", "Michael"
dic.add "keyName2", "John"
dic.add "keyName3", "Steve"

dic.Item("keyName2") <-- gives you "John"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文