按列名称显示或列出数据单元格的值
.
.
.
OleDbDataReader getme = cmd.ExecuteReader();
while (getme.Read())
{
MessageBox.Show(getme.GetString(1));
}
.
.
.
我使用 getme.GetString(1) 在消息框中显示表格中特定单元格的值。我已经像往常一样打开数据库,但没有数据网格。
我如何使用列名而不是整数 ID、GetString(1) 或 GetInt32(0) 等?
是否可以选择具有列名而不是数值的列?
我正在 C#.NET 中执行此操作。
.
.
.
OleDbDataReader getme = cmd.ExecuteReader();
while (getme.Read())
{
MessageBox.Show(getme.GetString(1));
}
.
.
.
I've used getme.GetString(1) to be show the value of specific cell from the table in a message box. I've opened the database as usual and i don't have datagrid.
How can i use the column name instead of the integer ID, GetString(1) or GetInt32(0) or etc.?
isn't is possible to choose the column with column name instead of numerical value?
I am doing this in C#.NET.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这个:
http:// /msdn.microsoft.com/en-us/library/system.data.common.dbdatareader.getordinal%28v=VS.80%29.aspx
使用 GetOrdinal 方法。
Check this out:
http://msdn.microsoft.com/en-us/library/system.data.common.dbdatareader.getordinal%28v=VS.80%29.aspx
Use GetOrdinal Method.
您可以通过两种方式执行此操作:
getMe.GetOrdinal("column_name")
返回用于GetString
GetInt32
等的索引。如果您这样做,您可以通过在while
循环之外执行此操作来节省一些时间。OleDbDataReader
(您的getMe
)定义一个索引器。您可以使用getMe["column_name"]
,然后将其简单地转换为您想要的对象类型:请参阅 oleDbDataReader
You can do that two ways:
getMe.GetOrdinal("column_name")
returns the index to use forGetString
GetInt32
etc. If you do this, you can save some time by doing it outside thewhile
loop.OleDbDataReader
(your,getMe
) defines an indexer. You can usegetMe["column_name"]
, and then simple cast it to the type of object you want:See oleDbDataReader