简体   繁体   中英

C# using odbcDataReader: Change decimal seperator from “.” to “,” in Decimal value

When I try to assign a value from the odbcDataReader to a 'Decimal' variable it removes the decimal seperator ",". The value itself is stored as a decimal in the database I'm retrieving data from. I have tried to use the CultureInfo like this:

   CultureInfo myCI = new CultureInfo("en-US", false);
   myCI.NumberFormat.NumberDecimalSeparator = ",";
   OdbcDataReader Reader = null;
   OdbcCommand cmd = new OdbcCommand("...");
   cmd.Connection = cn;
   Reader = cmd.ExecuteReader();
          if (Reader.HasRows)
                {
                    while (Reader.Read())
                    {
                        *(decimal variable)* p = Convert.ToDecimal(Reader["Decimal Value"], myCI);
                    }
                }

I'm located in the EU if that makes sense, our default decimal seperator is "," but I have to connect to another DB that uses "." as decimal seperator so I cannot change the default decimal settings to ",", I only need to have THIS value use "," as a decimal seperator. I feel like my problem is in the CultureInfo but cannot find a clear solution.

Hope this makes sense, thanks.

you can try it;

while (Reader.Read())
{
*(decimal variable)* p = Convert.ToDecimal(Reader["Decimal Value"], myCI);
}

to

while (Reader.Read())
{
    decimal p = 0;

    decimal.TryParse(Reader["ColumnName"].ToString().Replace('.',','), out p);
}

OR

while (Reader.Read())
{
    double p = double.Parse(Reader["ColumnName"].ToString(), CultureInfo.InvariantCulture);
}

The Decimal variable was a wrong choice of me, I have changed it to a double and the code p = Reader.GetDouble(Reader.GetOrdinal("Value")); works as I expected it to do.

Thanks for the information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM