gpt4 book ai didi

c# - SQLCommand 的返回值

转载 作者:太空宇宙 更新时间:2023-11-03 21:45:34 27 4
gpt4 key购买 nike

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

最佳答案

将此行 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

关于c# - SQLCommand 的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17186340/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com