我试图像这样处理 DbNull 异常:
string sql_com_sumcastka = "SELECT SUM(price) AS sumprice FROM kliplat WHERE akce='" + zakce.Text + "' AND year=" + year;
SqlCommand sc2 = new SqlCommand(sql_com_sumprice, spojeni);
spojeni.Open();
if (sc2 != DBNull.Value)
{
int result = Convert.ToInt32(sc2.ExecuteScalar());
}
else
{
int result = 0;
}
spojeni.Close();
string sql_com_update_sum = "UPDATE zajezd SET s_prijmy=@s_prijmy WHERE akce='"+zakce.Text+"' AND year="+year;
SqlCommand sc3 = new SqlCommand(sql_com_update_sum,spojeni);
sc3.Parameters.AddWithValue("@s_prijmy", result );
spojeni.Open();
sc3.ExecuteNonQuery();
spojeni.Close();
但是如果结果是 DBNull 我不知道如何正确处理我得到这个错误:Operator '"=' cannot be applied to operands of type system.Data.SqlClient.SqlCommand and System.Dbnull
和
当前上下文中不存在名称“结果”
我的问题是这行代码:
if (sc2 != DBNull.Value)
{
int result = Convert.ToInt32(sc2.ExecuteScalar());
}
else
{
int result = 0;
}
感谢您的帮助。