使用SqlDataReader该类,如果有的话,它们之间的功能差异:
(string) dataReader["MyFieldName"];
Run Code Online (Sandbox Code Playgroud)
和
dataReader.GetString(dataReader.GetOrdinal("MyFieldName"));
Run Code Online (Sandbox Code Playgroud)
cas*_*One 14
抛开问题,对于单一呼叫,没有.索引器将调用DbDataReader.GetOrdinal然后调用适当的Get方法来获取值(请注意,Get使用序数调用方法比使用带有字段名称的索引器更快).
但是,这将导致每次都查找序数.如果你通过了多项纪录中只进,只读方式迭代(这究竟是什么DbDataReader情况下是为了做),那么你可以这样做,它只是减少这种查询的开销一次.
你可以这样做:
// Move to the first record. If no records, get out.
if (!dataReader.Read()) return;
// Before the loop. Can do this for any other fields being
// accessed in the loop as well.
int myFieldNameOrdinal = dataReader.GetOrdinal("MyFieldName");
// Process the records. Remember, already on the first record, so
// use do/while here.
do
{
// Do something with your field.
Console.WriteLine(dataReader.GetString(myFieldNameOrdinal));
} while (dataReader.Read());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6004 次 |
| 最近记录: |