asp.net 返回记录集作为关联数组
我正在尝试深入研究 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你快到了。这是你必须改变的。
You almost there. Here is what you have to change.
您是否尝试过使用
[0]["PageID"]
?由于
RS[0]
是一个数组,因此对sqlText.Text
的分配将不起作用。如果 sqlText 是文本框,请尝试以下操作:
Have you tried using
[0]["PageID"]
?Your assignment to
sqlText.Text
will not work sinceRS[0]
is an array.Try this if sqlText is a TextBox:
C# 本身不支持关联数组。
sqlText.Text = RS[0]["PageID"].ToString();
的问题是 RS[0] 获取了 ArrayList 的第一条记录,该记录是一个类型的数组对象 -object[]
。但是object[]
没有接受字符串的索引器 - 您无法为object[]
数组的索引指定字符串值。如果您坚持使用对象数组,则必须使用项目值的索引值。例如,
第一个
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[]
. Butobject[]
doesn't have an indexer that takes a string -- you can't specify a string value for theobject[]
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,
The first
0
refers to the row, as it is an index into theArrayList
.The second
0
refers to the column, as it is an index into anobject[]
you created on the line after yourwhile (DbReader.Read())
line.The second index will be the columns in the order returned by the
DbReader
.