运用ExcecuteScalar方法时出错,帮忙改正啊!
代码如下:
SqlConnection conn = new SqlConnection("server=localhost;database=shoupiao;uid=sa;pwd=");
string strsql = "select sum(price) from chpyish where shijian='time1'";
conn.Open();
SqlCommand cmd = new SqlCommand(strsql, conn);
double d = (double)cmd.ExecuteScalar();
this.Label4.Text = Convert.ToString(d);
conn.Close();
异常详细信息: double d = (double)cmd.ExecuteScalar();这行指定的转换无效。
------解决方案--------------------
首先得判断ExecuteScalar()返回的值是否为null
double d = 0;
object result = cmd.ExecuteScalar();
if(result != null)
{
d = Convert.ToDouble(result);
}