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 ...