在 C# 中调用标量函数

Call Scalar function in C#

我有一个标量函数 运行 在 sql 中很好用。我需要在 c# 中调用该函数。标量函数在下面:

CREATE FUNCTION [dbo].[usfMGAHolidayCalendar](@MGAID INT, @MaskDate DATE)

RETURNS DATE
AS
/*Returns the NewFileMask*/
    BEGIN

        DECLARE @NewMaskDate DATETIME,
                @IsMGAHolidayCalendar INT = 0;

        SET @IsMGAHolidayCalendar =
        (
            SELECT COUNT(HolidayDate)
            FROM xml.MGAHolidayCalendar
            WHERE HolidayDate = @MaskDate
            AND MgaId = @MGAID  
        );

        IF @IsMGAHolidayCalendar > 0
                /*Return @NewMaskDate (@MaskDate + 1)*/
                SET @NewMaskDate = DATEADD(dd, 1,@MaskDate)
        
            ELSE
                /*Return the original @MaskDate as @NewMaskDate*/
                SET @NewMaskDate = @MaskDate

        /*Sometimes there are two holidays in a row (e.g. Thanksgiving & Thanksgiving Friday)*/
        /*Check the table for the NewMaskDate*/
        SET @IsMGAHolidayCalendar =
        (
            SELECT COUNT(HolidayDate)
            FROM xml.MGAHolidayCalendar
            WHERE HolidayDate = @NewMaskDate
            AND MgaId = @MGAID
        );

        IF @IsMGAHolidayCalendar = 1
                /*Return @NewMaskDate (@NewMaskDate + 1)*/
                SET @NewMaskDate = DATEADD(dd, 1,@NewMaskDate)
            ELSE
                /*Return the original @NewMaskDate as @NewMaskDate*/
                SET @NewMaskDate = @NewMaskDate

        RETURN @NewMaskDate;

我开始在 C# 中调用 Scalar 函数,这就是我目前所做的。我需要一些指导/帮助才能完成它。

//调用dbo.usfGetMGAHolidayCalendar和returnNewMaskDate//

    static void Main(string[] args)
    {
        //Set the connection string//

     try
        {
            //sql connection object
            using (SqlConnection conn = new SqlConnection(connString))
            {
                //define the query text
                string query = @"SELECT dbo.usfGetMGAHolidayCalendar(@MGAID, @MaskDate) As NewMaskDate;";

                //parameter value will be set from command line
                SqlCommand cmd = new SqlParameter("dbo.usfGetMGAHolidayCalendar", conn);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add("@MGAID", SqlDbType.Int);
                cmd.Parameters.Add("@MaskDate" , SqlDbType.Date);
               

我得到了你的关心,根据你的情况你可以尝试如下:

SQL 函数:

CREATE FUNCTION CallFunctionFromCSharp (@EmpId INT)
RETURNS INT
AS
BEGIN
    DECLARE @EmpProjectNumber INT;

    SELECT @EmpProjectNumber =  (SELECT WsEmployeeNumber FROM WsEmployee WHERE WsEmployeeId =@EmpId)

    IF (@EmpProjectNumber IS  NULL)
        SET @EmpProjectNumber = 0;

    RETURN @EmpProjectNumber;
END

这个函数Return一个像下面这样的数字:

在 C# 中调用

 using (var connection = new SqlConnection("Server=ServerName;Database=YourDatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true"))
                {
                    int employeeId = 1;
                    string projectNumber;
                    connection.Open();
                    var command = connection.CreateCommand();
                    command.CommandText = "SELECT dbo.CallFunctionFromCSharp(@EmpId) AS projectNumber";
                    command.Parameters.AddWithValue("@EmpId", employeeId);
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        projectNumber = reader["projectNumber"].ToString(); // Remember Type Casting is required here it has to be according to database column data type
                      
                    }
                    reader.Close();
                    command.Dispose();
                    connection.Close();

                }

Note: You have two parameter so you can write like this: dbo.CallFunctionFromCSharp(@EmpId,@anotherParam) then pass the parameter like this command.Parameters.AddWithValue("@anotherParam", anotherParamValue);

输出:

Note: Be double check the return type and parameter type if you encounter any exception.

希望它能帮助您实现目标。