Is it better to use the column name or column index on .Net DataSets?

tpower picture tpower · Jan 23, 2009 · Viewed 8.7k times · Source

When retrieving values from a DataRow is it better to use the column name or column index?

The column name is more readable and easier to maintain:

int price = (int)dr["Price"];

While column index is just faster (I think):

int price = (int)dr[3];

Would using column names break if you decide to obfuscate the database?

Answer

tvanfosson picture tvanfosson · Jan 23, 2009

I generally prefer readability and understanding over speed. Go with the name. You could (should) use string constants that can be updated in one place if you decide to change database column names.