浮动我在这里有这个方法,这将让我从数据库中浮动:ASP.NET充分利用数据库
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; }
}
检查从SqlCommand返回的类型,然后我们可以决定如何转换它。 –
SQL Db中的底层数据类型和构造,moveIn和yearEnd的实际数据是什么?你可以发布样本吗?另外,您希望使用**参数化查询**来防止[SQL注入攻击](https://en.wikipedia.org/wiki/SQL_injection)。 – Win