1

我有这个 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();
4

1 回答 1

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
}

有关该方法的更多信息在这里

于 2013-06-19T08:25:42.237 回答