![](/img/trans.png)
[英]System.InvalidCastException: Unable to cast object of type 'System.Double' to type 'System.Int32' in code
[英]System.InvalidCastException: 'Unable to cast object of type 'System.Double' to type 'System.Single'.'
嗨,我在 _obj.Add(new data()... 這是我的代碼
SqlCommand com = new SqlCommand("sp_get_a", con)
{
CommandType = CommandType.StoredProcedure
};
_obj = new List<n>();
con.Open();
SqlDataReader rdr = com.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
_obj.Add(new data() { Id = rdr.GetInt32(0), na = rdr.GetString(1), Al = rdr.GetString(2), Sc = rdr.GetFloat(3), So = rdr.GetInt32(4), Ta = rdr.GetInt32(5), Ai = rdr.GetInt32(6), Sh = rdr.GetInt32(7) });
}
}
else
{
throw new Exception("rdr don't have any rows");
}
con.Close();
我的存儲過程
CREATE PROCEDURE sp_get_a
AS SELECT Id, Na, Al, Sc, So, Ta, Ai, Sh FROM x 我沒有任何雙精度值,最接近雙精度的是 Sc(float)。 所以我嘗試了 Sc = Convert.ToSingle(rdr.GetFloat(3))。 我在哪里做錯了? 編輯-我的 model
public class n
{
public int Id { get; set; }
public string Na { get; set; }
public string Al { get; set; }
public float Sc { get; set; }
public int So { get; set; }
public int Ta { get; set; }
public int Ai { get; set; }
public int Sh { get; set; }
}
您需要將Sc
的數據類型從float
更改為double
Double SQL 服務器real
數相當於 C# Single
和 SQL 服務器float
相當於Double
。
你也應該考慮@jdweng點
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.