2016-02-04 45 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」));