在C#中调用SQL定义的函数

时间:2013-06-11 14:47:25

标签: 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

我想在我的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;
}

3 个答案:

答案 0 :(得分:37)

你不能只调用函数名,你需要编写一个使用UDF的内联SQL语句:

SqlCommand Totalf = new SqlCommand("SELECT dbo.Tcupom(@code)", conex1);

并删除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;
}

答案 1 :(得分:3)

...

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();
}

...

答案 2 :(得分:0)

我想以与调用存储过程相同的方式调用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;
            }
        }

    }

标量函数代码如下:

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