2016-02-04 38 views
0

浮动我在这里有这个方法,这将让我从数据库中浮动: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; } 
    } 
+0

检查从SqlCommand返回的类型,然后我们可以决定如何转换它。 –

+0

SQL Db中的底层数据类型和构造,moveIn和yearEnd的实际数据是什么?你可以发布样本吗?另外,您希望使用**参数化查询**来防止[SQL注入攻击](https://en.wikipedia.org/wiki/SQL_injection)。 – Win

回答

1

请尝试

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

试试看你的IDE的不同的建议,你会得到正确的。

+0

我得到这个错误:'''当前上下文中不存在'浮点'' – user979331

0

尝试使用getfloat item.construction = reader.GetFloat(reader.GetOrdinal(“0”));