2

I want to retrieve data by column from DataReader.

Now I'm using like this,

AdsCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT a,b,c,d FROM testTable";
AdsDataReader reader = cmd.ExecuteReader();

reader.Read();
string columnA = reader.GetValue(0).ToString(); // I want to use column name instead of index number

is there any way to get data by column name? like

string columnB = reader["B"].getValue(); 

Thank you!

Expert wanna be
  • 10,218
  • 26
  • 105
  • 158
  • There is a post asking a [related question about this](http://stackoverflow.com/questions/2882280/datareader-hardcode-ordinals). I posted some numbers comparing the two access methods if you are curious. – Mark Wilkins Apr 20 '12 at 21:58

1 Answers1

8

Did you try this:

string columnA = Convert.ToString(reader["B"]);
dcp
  • 54,410
  • 22
  • 144
  • 164