Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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# 从SQL连接返回输出参数_C#_Sql_Sql Server_Parameters - Fatal编程技术网

C# 从SQL连接返回输出参数

C# 从SQL连接返回输出参数,c#,sql,sql-server,parameters,C#,Sql,Sql Server,Parameters,如果不使用存储过程,这是可能的还是正确的方法: conn.CommandText = "set @TotalPts = select SUM (pts) from cars where completedDate is not null and customerID = @customerID"; conn.AddParam("@customerID", customerID); conn.AddOutputParam("@Tota

如果不使用存储过程,这是可能的还是正确的方法:

conn.CommandText = "set @TotalPts = select SUM (pts) from cars where completedDate is not null and customerID = @customerID";
                conn.AddParam("@customerID", customerID);
                conn.AddOutputParam("@TotalPts", SqlDbType.Int);

                return (int)conn.OutputParamValue;

我想取回总和,这样我就可以用我的C方法返回它。我不确定如何在字符串中设置输出参数。

在completedDate不为null且customerID=@customerID的情况下,使用ExecuteScalar和select SUM pts from cars


它将为您提供第一行/列中的内容,这正是您所需要的。

您可以使用以下ExecuteScalar方法

conn.CommandText = select SUM (pts) 
                    from cars 
                    where completedDate is not null 
                           and customerID = @customerID";

obj count=cmd.ExecuteScaler();
使用Convert.ToInt32函数将该计数转换为整数

我不知道如何在字符串中设置输出参数

select @TotalPts = SUM(pts) from ...