关于sql server:Dapper Execute返回-1值?

Dapper Execute returns -1 value?

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

1
2
3
4
5
6
7
8
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的相同查询如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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。 怎么了


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

https://dapper-tutorial.net/execute

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

我想说你应该在dapper中使用某些在幕后使用ExecuteScalar的东西-看看Dapper中是否有ExecuteScalar以及我对问题的评论的链接,以了解如何转换计数查询(也在注释中建议) int书籍结果,其中0为假,其他为真