ASP.NET 从数据库获取浮点数

本文关键字:获取 浮点数 数据库 NET ASP | 更新日期: 2023-09-27 18:33:10

我这里有这个方法,它将从数据库中获得一个浮点数:

public List<AvidClass> getAvidByJob(string customerID)
        {
            AvidCell = new List<AvidClass>();
            connection = new SqlConnection(connectionString);
            command = new SqlCommand(@"SELECT construction, moveIn, yearEnd FROM AvidRating WHERE CustomerID = '" + customerID + "'");
            command.Connection = connection;
            command.Connection.Open();
            SqlDataReader dataReader = command.ExecuteReader();
            while (dataReader.Read())
            {
                AvidClass item = new AvidClass();
                item.construction = (float)dataReader[0];
                item.moveIn = (float)dataReader[1];
                item.yearEnd = (float)dataReader[2];
                AvidCell.Add(item);
            }
            connection.Close();
            return AvidCell;
        }

但我收到此错误:

 Specified cast is not valid.

我做错了什么?

这是我的班级

public class AvidClass
    {
        public string community { get; set; }
        public string lot { get; set; }
        public int customerid { get; set; }
        public string user { get; set; }
        public float construction { get; set; }
        public float moveIn { get; set; }
        public float yearEnd { get; set; }
        public DateTime dateCreated { get; set; }
        public DateTime dateModified { get; set; }
        public string createdBy { get; set; }
        public string modifiedBy { get; set; }
    }

ASP.NET 从数据库获取浮点数

请尝试

Float.Parse(DataReader[0].toString());

只需尝试IDE的不同建议,您就会得到正确的建议。

尝试使用 getfloat项目.构造 = 读取器。GetFloat(reader.GetOrdinal("0"));