ale*_*ero 17 c# sql visual-studio-2010 sql-server-2008
我在TSQL中编写了这个标量函数:
create function TCupom (@cupom int)
returns float
as
begin
declare @Tcu float;
select @Tcu = sum (total) from alteraca2 where pedido = @cupom
if (@tcu is null)
set @tcu = 0;
return @tcu;
end
Run Code Online (Sandbox Code Playgroud)
我想在我的C#代码中调用此函数.这是我到目前为止所拥有的:
public void TotalCupom(int cupom)
{
float SAIDA;
SqlDataAdapter da2 = new SqlDataAdapter();
if (conex1.State == ConnectionState.Closed)
{
conex1.Open();
}
SqlCommand Totalf = new SqlCommand("Tcupom", conex1);
SqlParameter code1 = new SqlParameter("@code", SqlDbType.Int);
code1.Value = cupom ;
Totalf.CommandType = CommandType.StoredProcedure ;
SAIDA = Totalf.ExecuteScalar();
return SAIDA;
}
Run Code Online (Sandbox Code Playgroud)
Cur*_*urt 40
你不能只调用函数名,你需要编写一个使用UDF的内联SQL语句:
SqlCommand Totalf = new SqlCommand("SELECT dbo.Tcupom(@code)", conex1);
Run Code Online (Sandbox Code Playgroud)
并删除CommandType
,这不是存储过程,它是用户定义的函数.
在所有:
public void TotalCupom(int cupom)
{
float SAIDA;
SqlDataAdapter da2 = new SqlDataAdapter();
if (conex1.State == ConnectionState.Closed)
{
conex1.Open();
}
SqlCommand Totalf = new SqlCommand("SELECT dbo.Tcupom(@code)", conex1);
SqlParameter code1 = new SqlParameter("@code", SqlDbType.Int);
code1.Value = cupom;
SAIDA = Totalf.ExecuteScalar();
return SAIDA;
}
Run Code Online (Sandbox Code Playgroud)
...
try
{
if (connectionState != ConnectionState.Open)
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "udfName";
cmd.CommandType = CommandType.StoredProcedure;
foreach (var cmdParam in sqlParams)
{
cmd.Parameters.Add(cmdParam);
}
var retValParam = new SqlParameter("RetVal", SqlDbType.Int)
{
//Set this property as return value
Direction = ParameterDirection.ReturnValue
};
cmd.Parameters.Add(retValParam);
cmd.ExecuteScalar();
retVal = retValParam.Value;
}
}
finally
{
if (connectionState == ConnectionState.Open)
conn.Close();
}
...
Run Code Online (Sandbox Code Playgroud)
我想以与调用存储过程相同的方式调用 SQL 函数,即使用 DeriveParameters 然后设置参数值。事实证明,这适用于开箱即用的标量函数,您可以使用 ExecuteNonQuery 并读取 RETURN_VALUE。请参阅下面的示例代码:
public int GetLookupCodeFromShortCode(short tableType, string shortCode)
{
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Default"].ConnectionString))
{
conn.Open();
using (var cmd = new SqlCommand("dbo.fnGetLookupCodeFromShortCode", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 30;
SqlCommandBuilder.DeriveParameters(cmd);
cmd.Parameters["@sintTableType"].Value = tableType;
cmd.Parameters["@vchrShortCode"].Value = shortCode;
cmd.Parameters["@chrLanguage"].Value = "en";
cmd.Parameters["@chrCountry"].Value = "en";
cmd.ExecuteNonQuery();
return (int)cmd.Parameters["@RETURN_VALUE"].Value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
标量函数代码如下所示:
CREATE FUNCTION [dbo].[fnGetLookupCodeFromShortCode]( @sintTableType SMALLINT, @vchrShortCode VARCHAR(5), @chrLanguage CHAR(2), @chrCountry CHAR(2))
RETURNS INT
AS
BEGIN
DECLARE @intLookupCode INT
SELECT @intLookupCode = LV.intLookupCode
FROM
tblLookupValueDesc LVD
INNER JOIN tblLookupValue LV ON LV.sintLookupTableType = LVD.sintLookupTableType AND LV.intTableKey = LVD.intTableKey
WHERE
LVD.sintLookupTableType = @sintTableType
AND LVD.vchrShortCode = @vchrShortCode
AND LVD.chrCountry = @chrCountry
AND LVD.chrLanguage = @chrLanguage
RETURN @intLookupCode
END
GO
Run Code Online (Sandbox Code Playgroud)