0

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()?

user1246576
  • 51
  • 1
  • 8
  • `What happens if they are stored in a varchar and try to use .GetDecimal()?` What happened when you tried it? – mjwills Jul 13 '18 at 12:11
  • 2
    "What happens if they are stored in a varchar and try to use .GetDecimal()?" You get a big fat exception saying that your column doesn't have the expected type, that's what. `SqlDataReader` does not perform conversions between any data types. Even (say) reading an `INT` value with `GetInt64` will fail, even though all `INT` values can be represented in an `Int64`. – Jeroen Mostert Jul 13 '18 at 12:45
  • Unfortunately, `string` (`varchar`, etc) are *easy types to deal with* because "everything can be treated as a string"; The unfortunate aspect is that they're often the *least appropriate* type to use. – Damien_The_Unbeliever Jul 13 '18 at 12:51

1 Answers1

4

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?

René Vogt
  • 43,056
  • 14
  • 77
  • 99