循环列名并将它们放入变量中。 ASP
我需要能够循环列名并将列名及其值放入变量中。我的代码看起来像这样。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您应该使用 GetRows 方法。返回一个二维数组。您不需要通过 GetRows 获得列名称。
考虑一下:
I think you should use GetRows method. Returns a two-dimensional array. You don't need column names through GetRows.
Consider this:
如果要存储字段名称,可以使用字典并将每个字段名称作为键,然后将每列的值作为数组。
首先,声明字典:
现在使用这两个循环填充数据:
最后,您可以使用以下代码显示所有列名称:
或者显示特定列的值:
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:
Now populate it with data using those two loops:
And finally, you can show all the column names using such code:
Or show the values for specific column:
使用上面的代码,并将名称/值对存储在会话中,例如 session("table." & fieldname) = rs(fieldname)。我经常用它来存储用户、客户、供应商等信息。也像缓存一样工作。您可以创建像 reloadCustomer(customerNumber) 这样的子例程来重新加载。这样你就不会去数据库获取基本信息。更改时更新它...
字典对象:
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: