Skip to content
Advertisement

DataReader.GetString() via columnname

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

Advertisement

Answer

You’re looking for the GetOrdinal method:

I generally cache the ordinals in an anonymous type for performance and readability:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement