简体   繁体   English

无法从SQL Server数据库读取浮点值

[英]Can't read float value from SQL Server database

I use SQL Server to build my database and SqlDataReader to read data from it. 我使用SQL Server构建数据库,并使用SqlDataReader从中读取数据。

command.Connection = cn;
command.CommandText = "SELECT * FROM test";
SqlDataReader rd = command.ExecuteReader();
while(rd.Read())
{
    double d = (double) rd.GetValue(0);
}

The column (0) I am trying to get value from is a 'float' type and has value '3.5' . 我试图从中获取值的列(0)是'float'类型的,并且具有值'3.5'。 As mapping data type from this MSDN link , the type of the object returned by rd.GetValue(0) must be 'double'. 作为来自此MSDN链接的映射数据类型,rd.GetValue(0)返回的对象的类型必须为'double'。 But the code above returns to variable 'd' value '0.0'. 但是上面的代码返回到变量'd'值'0.0'。 I tried this line: 我尝试了这一行:

double d = Convert.ToDouble(rd.GetValue(0));

But it still returns '0.0' to variable 'd'. 但是它仍然将'0.0'返回到变量'd'。

I tried searching on Google and StackOverflow but there is no result. 我尝试在Google和StackOverflow上搜索,但没有结果。

What am I missing? 我想念什么? Help me! 帮我!

As it is now, your code iterates over all the records (if there are many) an takes the last entry, which since you have no order by clause, may differ in every query execution. 现在,您的代码将遍历所有记录(如果有很多记录),并接受最后一个条目,由于您没有order by子句,因此在每次查询执行中可能会有所不同。 If indeed you want to only take 1 value, use ExecuteScalar together with an order by clause: 如果确实只想获取1个值,则将ExecuteScalarorder by子句一起使用:

command.Connection = cn;
command.CommandText = "SELECT  TOP 1 * FROM test order by myfield desc"; //or asc
double result = (double)command.ExecuteScalar();

Otherwise have all the result saved in a list: 否则,将所有结果保存在列表中:

... ...

List<double> result = new List<doulbe>();
while(rd.Read())
{
    result.Add(double.Parse(rd[0].ToString());
}

Finally, if you need only the 1st field, for performance reasons, is far better not to use * but explicit set the field you want: 最后,出于性能原因,如果只需要第一个字段,最好不要使用*,而是显式设置所需的字段:

"SELECT  TOP 1 myfield  FROM test order by myfield desc"; //or asc

you can try it; 你可以试试;

double d = (double) rd.GetValue(0);

to

double d = 0;
double.TryParse(rd["ColumnName"].ToString().Replace('.',','),out d);    

OR: 要么:

double d = double.Parse(rd["ColumnName"].ToString(), CultureInfo.InvariantCulture); 

This here works fine for me, im getting 3,5 in my list 这对我来说很好,我的列表中有3,5

List<double> columnData = new List<double>();
            using (SqlConnection connection = new SqlConnection("Server=EGC25199;Initial Catalog=LegOgSpass;Integrated Security=SSPI;Application Name=SQLNCLI11.1"))
            {
                connection.Open();
                    string query = "SELECT * FROM [dbo].[floattable]";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            columnData.Add(reader.GetDouble(0));

                        }
                    }
                }
            }

Oh I have found the answer. 哦,我找到了答案。 Nothing wrong with the code I wrote. 我编写的代码没有错。 The problem is that I place the breakpoint on the 'double d = (double) rd.GetValue(0)' line. 问题是我将断点放在“ double d =(double)rd.GetValue(0)”行上。 That is, 'd' value is not assigned yet so that on the debug screen it returns '0.0'. 也就是说,尚未分配“ d”值,因此在调试屏幕上它将返回“ 0.0”。 Sorry for this mistake and thank you all Stack-Over-flowers for spending your time helping me!!!! 对于这个错误,我们深表歉意,并感谢所有花满您的时间来帮助我的Stack-Over-flowers!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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