DataReader.GetString() 通过列名
Dictionary Fields = new Dictionary(); for (int i = 0; i < reader.FieldCount; i++) { Fields.Add(reader.GetName(i), i); } this._MyField1 = reader.GetString(Fields["field1"]); this._Myfield2 = reader.GetInt16(Fields["field2"]);
这样做让我想哭,但除了这种方式之外,我似乎无法弄清楚如何通过列名使用特定于类型的检索方法。请告诉我有更好的方法。这是专门针对 DB2 的,但如果可能的话,我希望该解决方案也适用于 MS Sql
Dictionary Fields = new Dictionary(); for (int i = 0; i < reader.FieldCount; i++) { Fields.Add(reader.GetName(i), i); } this._MyField1 = reader.GetString(Fields["field1"]); this._Myfield2 = reader.GetInt16(Fields["field2"]);
doing this makes me want to cry but i can't seem to figure out how to use the type specfic retrieval methods by column name other than this way. please tell me there is a better way. this is specificly for DB2 but i would like the solution to work for MS Sql also if possible
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找
GetOrdinal< /code>
方法:
我通常将序号缓存在匿名类型中,以提高性能和可读性:
You're looking for the
GetOrdinal
method:I generally cache the ordinals in an anonymous type for performance and readability:
使用 Assembly
System.Data.SqlClient
中的SqlDataReader
,您可以执行以下操作(示例):对象
GetOrdinal(string)
的方法GetOrdinal(string)
code>SqlDataReader,接受您想要获取的列名并返回列号。该数字可用于方法
GetString(int)
Using
SqlDataReader
from AssemblySystem.Data.SqlClient
, you can do the following (example):The method
GetOrdinal(string)
from the objectSqlDataReader
, accept the column name you desire to get and return the column number.That number can be use for the method
GetString(int)