I have a scalar function which ran fine in sql. I need to call that function in c#. The scalar function is down below:
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;
I started calling the Scalar function in C# and this is what I have so far. I need some guidance/ help to completed it.
//Call dbo.usfGetMGAHolidayCalendar and return NewMaskDate//
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);