Dapper Execute返回-1值吗?

时间:2018-10-18 04:41:46

标签: sql-server dapper

以下SQL查询在给定值可用时返回1,在不可用时返回0,并且在SQL Server Management Studio中可以正常工作,

select 
    case 
       when exists (select * from [dbo].[user] 
                    where userName = 'admin' 
                      and password = 'admin') 
          then cast(1 as bit) 
          else cast(0 as bit)
    end

与精简工具ORM相同的查询如下:

public int login(string userName, string password)
{
        string sql = "select case when exists(select * from user where userName = @usernamepara and password = @passwordpara) then cast(1 as bit) else cast(0 as bit )end";

        using(IDbConnection conn = dbConnection)
        {
            conn.Open();

            var res = conn.Execute(sql, 
                                   param: new { usernamepara = userName, 
                                                passwordpara = password });
            conn.Close();
            return res;
        }
    }

但是,调用此方法时,对于匹配和不匹配的记录,它返回-1。怎么了?

1 个答案:

答案 0 :(得分:0)

可能是因为您使用了错误的执行:

https://dapper-tutorial.net/execute

说EXECUTE将返回受影响的行数,并且该文档清楚表明execute并不用于返回行的查询

我想说的是,您应该在dapper中使用在幕后使用ExecuteScalar的东西-请参见is there an ExecuteScalar in Dapper以及该问题的评论链接,以了解如何进行计数查询(在注释)将图书结果设为0,否则为true