0

我正在尝试深入研究 asp.net 和 C# 并遇到一些问题;

我已经建立了一个访问 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;

我想没关系,我的问题是如何显示返回的数据;在 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.

被.net 的复杂性蒙蔽了双眼,我没能在谷歌得到帮助。

问候,//t

4

3 回答 3

1

你快到了。这是你必须改变的。

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();
于 2011-04-26T21:19:33.463 回答
0

你试过使用[0]["PageID"]吗?

您的分配sqlText.Text将不起作用,因为RS[0]它是一个数组。

如果 sqlText 是一个文本框,试试这个:

sqlText.Text = RS[0]["PageID"].ToString(); 
于 2011-04-26T20:22:57.473 回答
0

C# 本身不支持关联数组。

问题sqlText.Text = RS[0]["PageID"].ToString();在于 RS[0] 到达 ArrayList 的第一条记录,这是一个 Object - 类型的数组 object[]。但是object[]没有一个接受字符串的索引器——你不能为object[]数组的索引指定一个字符串值。

如果您坚持使用对象数组,则必须对项目值使用索引值。例如,

sqlText.Text = RS[0][0].ToString();   // Row 0, Column 0

第一个0指的是行,因为它是ArrayList.
第二个0指的是列,因为它是object[]您在行后的行上创建的索引while (DbReader.Read())

第二个索引将是按返回顺序排列的列DbReader

于 2011-04-26T21:37:15.403 回答