Are .net SqlDataReader methods culture invariant

691 Views Asked by At

I can't really find a definitive answer to what happens if for example my decimals are stored in the database with different culture settings and I want to get them via a SqlDataReader into my program. Will the ',' and '.' always be correctly parsed and converted to my current culture as long as the data type in the SQL-DB is set to decimal?

What happens if they are stored in a varchar and try to use .GetDecimal()?

1

There are 1 best solutions below

0
René Vogt On

Are .net SqlDataReader methods culture invariant

SqlDataReader does not use any CultureInfo or something equivalent. It's not aware of cultures.

When your values are stored in the db as decimal, the SqlDataReader receives that decimal value. How they are (string-) represented in your app then depends how you do it: simply with dataReader["myColumn"].ToString() will use your current CultureInfo. If you want a different culture, you have to provide that explicitly.

What happens if they are stored in a varchar and try to use .GetDecimal()?

When they are stored as strings, you have a problem (at least if you don't know the culture these strings were created with). You have to use the correct CultureInfo to first parse those strings to get the correct values. But why would you store numbers as strings in your db, anyway?