我用SQL写了这个函数
alter function TVprest (@emitente int, @mes int, @ano int)
returns float
as
begin
declare @tcu float;
select @tcu = sum(cast(vtprest as money))
from ctrc
where emitente = @emitente and MONTH (EMISSAODATA ) = @mes
and YEAR (EMISSAODATA)=@ano and status = 'A'
if (@tcu is null)
set @tcu = 0;
return @tcu
end
并尝试使用此代码在 C# 中调用相同的函数:
public double TVprest (int emitente, int mess, int anno)
{
double saida;
SqlConnection abre1 = Tconex.GetConnection();
SqlDataAdapter da3 = new SqlDataAdapter();
if (abre1.State == ConnectionState.Closed) { abre1.Open(); }
SqlParameter emit = new SqlParameter("@emitente", SqlDbType.Int);
emit.Value = emitente;
SqlParameter mes = new SqlParameter("@mes", SqlDbType.Int);
mes.Value = mess;
SqlParameter ano = new SqlParameter("@ano", SqlDbType.Int);
ano.Value = ano;
SqlCommand TotalF = new SqlCommand("SELECT dbo.Tcupom(@emitente,@mes,@ano),", abre1);
TotalF.CommandType = CommandType.Text;
saida = Convert.ToDouble(TotalF.ExecuteScalar());
return saida;
}
运行时出现这个错误:
Failed to convert parameter value from SqlParameter to an Int32
怎么了?使用这些参数调用函数:
double Tvprest = impx.TVprest(504, 5, 2013);
lblVtprest.Text = Tvprest.ToString();
最佳答案
你还没有给你的命令添加参数
SqlCommand TotalF = new SqlCommand("SELECT dbo.Tcupom(@emitente,@mes,@ano),", abre1);
TotalF.Parameters.Add(emit);
TotalF.Parameters.Add(mes);
TotalF.Parameters.Add(ano);
saida = Convert.ToDouble(TotalF.ExecuteScalar());
但是,我认为您缺少解释问题中的内容。您有一个名为 TVprest 的函数,但您调用了一个 SELECT dbo.Tcupom
。不清楚 Tcupom
关于c# - C# 中具有 3 个参数的标量 sql 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17662799/