0

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.

4

1 回答 1

3

你有没有尝试过;

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

而不是您发布的第二个代码块?

于 2013-09-11T19:55:48.463 回答