C#,从MySQL数据库获取双重值的问题 [英] C#, problems with getting double values from MySQL database

查看:172
本文介绍了C#,从MySQL数据库获取双重值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MySQL数据库与表产品产品中的列称为价格,数据类型为 double



我需要从该列中检索值,因此我创建了一个读卡器等。

  MySQLCommand cmd = new MySQLCommand(SELECT Price FROM Products,connection); 
MySQLDataReader reader = cmd.ExecuteReaderEx();

if(reader.HasRows == true)
{
while(reader.Read()== true)
{
price = reader [ 价格])的ToString();
}
}

问题是价格未设置为预期值。如果数据库中的值为299.95,价格设置为29995.0。



任何想法为什么会发生这种情况?可以做些什么来解决它?

解决方案

这是因为toString()使用当前的CultureInfo!
这取决于文化,如果双面被逗号或点分隔。



CultureInfo



另请参阅 Stackoverflow问题!



如果您调试它,您应该看到,该读者[Price]返回一个Object(type = Object {double})。这里的价值是否正确?我想是这样,所以只需要做以下显示双重值:

  string display = double.Parse(reader [价格],CultureInfo.InvariantCulture).ToSring(CultureInfo.CurrentCulture); 
System.Diagnostics.Debug.WriteLine(display);


I have a MySQL database with the table "Products". A column in "Products" is called "Price" and has the datatype "double".

I need to retrieve the values from that column, so I create a reader, etc.:

MySQLCommand cmd = new MySQLCommand("SELECT Price FROM Products", connection);
MySQLDataReader reader = cmd.ExecuteReaderEx();

if (reader.HasRows == true)
{
  while (reader.Read() == true)
  {
    price = reader["Price"]).ToString();
  }
}

Problem is that price isn't set to the expected value. If the value in the database is "299.95", price is set to "29995.0".

Any idea why this is happening? And what can be done to fix it?

解决方案

This is, because toString() uses the current CultureInfo! It depends on the culture if a double is separated by a comma or a dot.

CultureInfo

See also this Stackoverflow Question!

If you debug it you should see, that reader["Price"] is returning an Object (type=Object{double}). Is here the value correct? I guess it is, so just make following to display the double-value:

string display = double.Parse(reader["Price"], CultureInfo.InvariantCulture).ToSring(CultureInfo.CurrentCulture);
System.Diagnostics.Debug.WriteLine(display);

这篇关于C#,从MySQL数据库获取双重值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆