我这里有这个方法,它会让我从数据库中获取一个浮点数:
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; }
}
答案 0 :(得分:1)
请尝试
Float.Parse(DataReader[0].toString());
尝试IDE的不同建议,你就会得到正确的建议。
答案 1 :(得分:0)
尝试使用getfloat item.construction = reader.GetFloat(reader.GetOrdinal(&#34; 0&#34;));