Last post Jul 15, 2008 07:55 PM by ABooth
Apr 03, 2008 08:33 PM|stevenjamesfrank65|LINK
In C#, when I'm using an SqlDataReader to run a simple select query and use the values returned I have a question about referencing the field values.
There seem to be a large number of "Get" type methods (i.e reader.GetValue(int i), etc.) All of them (that I have found so far, require the use of the index of the field (GetValue(0) is the first field and so on).
Is there a way to use the actual name of the field? (i.e. GetValue("myField"))? This helps code readability but the big reason is that changes to the underlying field list can completely hose up the application.
I know there are such functions in VB, but I just can't seem to find the equivelants in C#.
Apr 03, 2008 09:08 PM|XPSCodes|LINK
Yes you can
Apr 04, 2008 02:48 AM|stevenjamesfrank65|LINK
Just so I make sure I'm not an idiot here, I have it working like this:
Seeing as how GetOrdinal returns the ordinal position of the names column...and this seems to work...
Apr 04, 2008 06:35 AM|XPSCodes|LINK
Yes,You got it right
Jul 15, 2008 07:55 PM|ABooth|LINK
What's wrong with: -
int MyInt = (int) reader["TheIntegerField"];