c# - SQLCommand 的返回值

标签 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();

最佳答案

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

相关文章:

c# - 如何将 c# 日期时间变量插入 SQL Server

c# - 如何隐藏 dataGridView 中的特定列?

c# - Dapper.Contrib - InsertAsync 无法插入 HIERARCHYID 键

c# - 在 PayPal SandBox、MVC 中创建付款时出现 INTERNAL_SERVICE_ERROR

c# - 如何设置 CommandTimeout

sql-server - 撤销与拒绝 : what is the difference

c# - 无法将列名称放入GetString

c# - 在 TFS 和 visual studio 中管理数据库项目的最佳实践

c# - 如何在 Unity TileSystem 中停止平铺动画?

c# - 添加SqlParameter绑定(bind)LIKE '%@x%'