how can i loop through all of the columns of the OracleDataReader

leora picture leora · Jun 8, 2010 · Viewed 17.1k times · Source

I have the following code and i want to loop through all the fields in the result of this query and populate the dictionary called field.

Given a datareader is this possible?

            OracleCommand command = connection.CreateCommand();
            string sql = "Select * from MYTABLE where ID = " + id;
            command.CommandText = sql;

            Dictionary<string, string> fields = new Dictionary<string, string>();
            OracleDataReader reader = command.ExecuteReader();

Answer

MikeWyatt picture MikeWyatt · Jun 8, 2010

You should be able to do something like this:

Dictionary<string, string> fields = new Dictionary<string, string>();
OracleDataReader reader = command.ExecuteReader();

if( reader.HasRows )
{
    for( int index = 0; index < reader.FieldCount; index ++ )
    {
        fields[ reader.GetName( index ) ] = reader.GetString( index );
    }    
}