Вопрос

i created a stored proc to return a bool and i am trying to build its connection in my app (asp.net mvc c#) but i am not sure how i should execute the command to properly return the bool.

code:

public virtual bool CheckEmail(string email, int Id)
        {

            SqlCommand _command = new SqlCommand("dbo.CheckEmail");
            _command.Connection = DbInstance.SqlConnection;
            _command.CommandType = CommandType.StoredProcedure;
            _command.Parameters.Add(new SqlParameter { ParameterName = "Email", SqlDbType = SqlDbType.NVarChar, Value = email });
            _command.Parameters.Add(new SqlParameter { ParameterName = "Id", SqlDbType = SqlDbType.Int, Value = Id});

            ........
        }

I thought i would try:

var _result = DbInstance.ExecuteAsSingle<int>(_command, r => r.GetValueOrDefault<int>(0));

            if (_result > 0)
                return true;
            return false;

the error i would get is: {"Specified cast is not valid."}

any helpful tips would be great - thanks.

Это было полезно?

Решение

Have you tried;

return DbInstance.ExecuteAsSingle<bool>(_command, r => r.GetValueOrDefault<bool>(0));

Instead of that second code block you posted?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top