asp.net 返回记录集作为关联数组

发布于 2024-11-03 18:45:28 字数 902 浏览 1 评论 0原文

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

我已经构建了一个类来访问数据库数据(不要问为什么,这是一个作业)。

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

I'm trying to dig into asp.net and C# and have some problems;

I've built a class to access db-data (don't ask why, it's an assignment).

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:

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.

regards,
//t

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

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

发布评论

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

评论(3

蓝海 2024-11-10 18:45:28

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

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();

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();
澉约 2024-11-10 18:45:28

您是否尝试过使用[0]["PageID"]

由于 RS[0] 是一个数组,因此对 sqlText.Text 的分配将不起作用。

如果 sqlText 是文本框,请尝试以下操作:

sqlText.Text = RS[0]["PageID"].ToString(); 

Have you tried using [0]["PageID"]?

Your assignment to sqlText.Text will not work since RS[0] is an array.

Try this if sqlText is a TextBox:

sqlText.Text = RS[0]["PageID"].ToString(); 
李白 2024-11-10 18:45:28

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

sqlText.Text = RS[0]["PageID"].ToString(); 的问题是 RS[0] 获取了 ArrayList 的第一条记录,该记录是一个类型的数组对象 - object[]。但是 object[] 没有接受字符串的索引器 - 您无法为 object[] 数组的索引指定字符串值。

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

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

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

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

C# doesn't support associative arrays natively.

The problem with 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[]. But object[] doesn't have an indexer that takes a string -- you can't specify a string value for the object[] array's index.

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.
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.

The second index will be the columns in the order returned by the DbReader.

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