从SQLCommand返回值

时间:2013-06-19 08:24:08

标签: c# sqlcommand

我有这个SqlCommand,但我不知道,我如何给出返回值?

string param = Request.QueryString["id"];
System.Data.SqlClient.SqlConnection se = new System.Data.SqlClient.SqlConnection(GetConnectionString());
            {
                System.Data.SqlClient.SqlCommand cme;
                se.Open();
                cme = new System.Data.SqlClient.SqlCommand("select Sum(case when (Domaci_tym = 'Tupesy') then Domaci_branky else Hoste_branky end) from Zapas AS branky where (ID_zapas=@ID_zapas)", se);
                cme.Parameters.AddWithValue("@ID_zapas", param);
                cme.ExecuteNonQuery();
                se.Close();

1 个答案:

答案 0 :(得分:4)

将此行cme.ExecuteNonQuery();更改为:

double sum = 0d;
var result = cme.ExecuteScalar(); //returns object that needs casting to get proper value

if (result != null && double.TryParse(result.ToString(), out sum))
{
    //query returned valid sum
}

有关该方法的更多信息here