[英]asp.net return recordset as associated array
I'm trying to dig into asp.net and C# and have some problems; 我正在尝试深入研究asp.net和C#并遇到一些问题。
I've built a class to access db-data (don't ask why, it's an assignment). 我已经建立了一个访问db-data的类(不要问为什么,这是一个分配)。
public static class dbConn {
public static ArrayList dbGet(string odbcStr, string sql) {
OdbcConnection DbConnection = new OdbcConnection(odbcStr);
...
ArrayList rowList = new ArrayList();
...
while (DbReader.Read())
{
object[] values = new object[DbReader.FieldCount];
DbReader.GetValues(values);
rowList.Add(values);
}
....
return rowList;
I guess it's ok, my problem is how to show returned data; 我想没关系,我的问题是如何显示返回的数据; in about.aspx.cs:
在about.aspx.cs中:
void Page_Load(object sender, EventArgs e)
{
ArrayList RS = new ArrayList();
RS = dbConn.dbGet("DSN=mysqloverodbc", "select * from pages");
Array RSrow = RS[0];
sqlText.Text = RS[0];
//what I want here is to request [0]["PageID"] or similar.
Blinded by the complexity of .net, I've failed to get help at google. 由于.net的复杂性而蒙蔽,我未能在google上获得帮助。
regards, //t 问候,// t
You almost there. 你快到了 Here is what you have to change.
这是您必须更改的内容。
Array RSrow = RS[0] as Array;
int pageIDIndex = 0; // Note :you have to know the column index in the table.i.e If the table has three columns, then the column index starts from 0 to columns length-1
sqlText.Text = RSrow.GetValue(pageIndexID).ToString();
Have you tried using [0]["PageID"]
? 您是否尝试过使用
[0]["PageID"]
?
Your assignment to sqlText.Text
will not work since RS[0]
is an array. 由于
RS[0]
是一个数组, sqlText.Text
您无法分配给sqlText.Text
。
Try this if sqlText is a TextBox: 如果sqlText是TextBox,请尝试以下操作:
sqlText.Text = RS[0]["PageID"].ToString();
C# doesn't support associative arrays natively. C#本机不支持关联数组。
The problem with sqlText.Text = RS[0]["PageID"].ToString();
sqlText.Text = RS[0]["PageID"].ToString();
is that the RS[0] gets to the first record of the ArrayList, which is an array of type Object - object[]
. 是RS [0]到达ArrayList的第一条记录,它是Object-
object[]
类型的数组。 But object[]
doesn't have an indexer that takes a string -- you can't specify a string value for the object[]
array's index. 但是
object[]
没有带字符串的索引器-您不能为object[]
数组的索引指定字符串值。
If you are stuck with using an object array, you will have to use index values for the item values. 如果您坚持使用对象数组,则必须使用索引值作为项目值。 For example,
例如,
sqlText.Text = RS[0][0].ToString(); // Row 0, Column 0
The first 0
refers to the row, as it is an index into the ArrayList
. 前一个
0
指向该行,因为它是ArrayList
的索引。
The second 0
refers to the column, as it is an index into an object[]
you created on the line after your while (DbReader.Read())
line. 第二个
0
指向该列,因为它是在while (DbReader.Read())
行之后的行中创建的object[]
的索引。
The second index will be the columns in the order returned by the DbReader
. 第二个索引将是
DbReader
返回的顺序中的列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.