SQL Server Stored procedure output parameter value is null (entity framework)

agyaoht7  于 2023-10-15  发布在  其他
关注(0)|答案(1)|浏览(105)

I am trying to call a stored procedure from entity framework but I am unable to get the output parameter value. It is always returning null. Here is my code

object[] parameters = {
                    new SqlParameter("@Date", date),
                    new SqlParameter("@outDate", SqlDbType.Date) {Direction= ParameterDirection.Output}
                };
                int defval = context.Database.CommandTimeout.Value;
                context.Database.CommandTimeout = 1800;
                context.Database.ExecuteSqlCommand("dbo.TestDateFormatAndRegion @Date, @outDate", parameters);
                var NoOfSyncOperationsDeleted = parameters[1].ToString();

Can you please help.

Thanks

zaqlnxep

zaqlnxep1#

In the context.Database.ExecuteSqlCommand call, modified the SQL command to use EXEC to explicitly execute the stored procedure and add OUTPUT to the @outDate parameter.

object[] parameters = {
    new SqlParameter("@Date", date),
    new SqlParameter("@outDate", SqlDbType.Date) { Direction = ParameterDirection.Output }
};

int defaultTimeout = context.Database.CommandTimeout.Value;
context.Database.SetCommandTimeout(1800);

context.Database.ExecuteSqlCommand("EXEC dbo.TestDateFormatAndRegion @Date, @outDate OUTPUT", parameters);

var outputParameter = parameters[1].Value;

相关问题