Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SqlCommand SUM十进制输出问题_C#_Sql_Sql Server_Converter_Sqlcommand - Fatal编程技术网

C# SqlCommand SUM十进制输出问题

C# SqlCommand SUM十进制输出问题,c#,sql,sql-server,converter,sqlcommand,C#,Sql,Sql Server,Converter,Sqlcommand,我得到了这个代码,它在sqldb中SUM number(9,2)值,但是输出是System.Data.SqlClient.SqlCommand 我知道我必须以某种方式转换它,但不知道如何转换 你能给我提个建议吗?非常感谢您抽出时间 conn.Open(); SqlCommand sc = new SqlCommand("SELECT SUM(price) AS sumprice FROM client WHERE subkey="+key,conn); sc.Execut

我得到了这个代码,它在
sqldb
SUM number(9,2)
值,但是输出是
System.Data.SqlClient.SqlCommand

我知道我必须以某种方式转换它,但不知道如何转换

你能给我提个建议吗?非常感谢您抽出时间

    conn.Open();
    SqlCommand sc = new SqlCommand("SELECT SUM(price) AS sumprice FROM client WHERE subkey="+key,conn);
    sc.ExecuteNonQuery();
    conn.Close();

    textBox20.Text = sc.ToString();
}

您正在查看的是
ExecuteScalar()
而不是
ExecuteNonQuery()

为了正确编码

  • 参数化该值以避免
    sql注入
  • 使用
    使用
    语句进行适当的对象处理
  • 使用
    try catch
    块正确处理异常

我建议在何时使用
ExecuteNonQuery
以及何时不太使用时查找。。这是你可以很容易地在谷歌上搜索以供将来参考的东西
int _result = Convert.ToInt32(sc.ExecuteScalar());