如何避免null而改为整数0?
cmd.CommandText = "SELECT MAX(*) " +
"FROM [Test] " +
"WHERE ID = @id ;";
cmd.Parameters.AddWithValue("@id", ID);
cmd.ExecuteNonQuery();
Amount = (int)cmd.ExecuteScalar();
答案 0 :(得分:3)
首先你必须指定你想要calcul MAX SELECT MAX(column_name)
的列,你可以使用它:
object obj = cmd.ExecuteScalar();
Amount = (obj != null && obj != DBNull.Value) ? Convert.ToInt32(obj) : 0 ;