Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 错误的查询:靠近“)”的语法不正确_Asp.net_Sql Server 2008_Dapper - Fatal编程技术网

Asp.net 错误的查询:靠近“)”的语法不正确

Asp.net 错误的查询:靠近“)”的语法不正确,asp.net,sql-server-2008,dapper,Asp.net,Sql Server 2008,Dapper,我有ASP.NET应用程序,我们使用Dapper库。产生错误的代码如下所示: public bool CheckIfExists(IEnumerable<long> ticketGroups, long dateId, int userId) { bool bRetVal = false; string sql = "if exists (select * from T_TicketGroupsToChangePrice where SubTypeId = @SubT

我有ASP.NET应用程序,我们使用Dapper库。产生错误的代码如下所示:

public bool CheckIfExists(IEnumerable<long> ticketGroups, long dateId, int userId)
{
    bool bRetVal = false;
    string sql = "if exists (select * from T_TicketGroupsToChangePrice where SubTypeId = @SubTypeId and DateId = @dateId and UserId = @userId)";
    using (var conn = CreateSqlConnection())
    try
    {
        int rows = conn.Execute(sql, ticketGroups.Select(g => new { SubTypeId = g, UserId = userId, dateId }));
        if (rows > 0)
            bRetVal = true;
    }
    catch (SqlException ex)
    {
        throw new Exception("Error", ex);
    }

    return bRetVal;
}
当我运行应用程序时,它会在附近抛出exeption:error语法

如您所见,可以有更多具有相同日期和用户的票证IEnumerable类型


我不确定发生了什么。

这是因为以if开头的SQL是无效的,如果你想使用T-SQL,那么就必须编写整个if语句

我认为您需要一个简单的案例:

select case
       when exists (select * from T_TicketGroupsToChangePrice where SubTypeId = @SubTypeId and DateId = @dateId and UserId = @userId)
       then 1
       else 0
       end
您的查询如果存在,请从T_TicketGroupsToChangePrice中选择*,其中subscriptid=@subscriptid和DateId=@DateId和UserId=@UserId返回一些数据,如果表中有一些数据,那么它需要处理一些数据。与编程中的if-else条件一样,我们可以将其修改为:

if exists 
(select * from T_TicketGroupsToChangePrice where SubTypeId = @SubTypeId and DateId = @dateId and UserId = @userId) 
Print 'Have Data'
 else 
Print 'Don't Have data'
重写代码:

public bool CheckIfExists(IEnumerable<long> ticketGroups, long dateId, int userId)
{
    bool bRetVal = false;
    string sql = "if exists (select * from T_TicketGroupsToChangePrice where SubTypeId = @SubTypeId and DateId = @dateId and UserId = @userId) Print '**your code to execute if exist data**' else Print '**your code to execute if doesnot exist data**'";
    using (var conn = CreateSqlConnection())
    try
    {
        int rows = conn.Execute(sql, ticketGroups.Select(g => new { SubTypeId = g, UserId = userId, DateId = dateId }));
        if (rows > 0)
            bRetVal = true;
    }
    catch (SqlException ex)
    {
        throw new Exception("Error", ex);
    }

    return bRetVal;
}
此链接将为您提供更多帮助:

如果结果取决于行数而不是SQL返回的内容,则可以尝试以下方法:

if exists ([whatever]) select 1
这是可行的,因为如果没有匹配的值,则不会返回任何记录集,并且受影响的记录计数为零

您也可以尝试更简单的方法:

select 1 
from T_TicketGroupsToChangePrice 
where SubTypeId = @SubTypeId 
  and DateId = @dateId 
  and UserId = @userId;
但这样做的缺点是,无论有多少记录,都会返回一行。这可能是很多,取决于应用程序和上下文,并且在任何情况下,您都不希望提取您不打算使用的数据

我不建议使用CASE语句,因为SELECT CASE EXISTS[无论如何],那么1 END仍将返回一条记录,并且即使不存在任何记录,受影响的记录计数也将为1


顺便说一下,原始SQL的问题是:语句不完整。你是说如果存在。。。但你永远都不会用相当于一个小时的时间来完成它。您需要说明是否存在选择1或类似的选项。

不是答案,但您的逻辑似乎有点奇怪。如果存在,您似乎要求db提供布尔响应,但随后根据预期的行数计算结果。是。如果返回的行数大于0,则表示该行存在,并且方法返回true。否则是错误的。你为什么说这很奇怪?但是你问db的问题不是“有多少行?”。你是在问‘有什么争吵吗?’。不同的事情。只是一个很小的观察,没错。我问是否有行,但Dapper方法的语法是这样的。问题是Execute方法返回受影响的行数。对不起。我没注意到。然后使用ExecuteScalar。出现一个错误:必须声明标量变量@SubsubId。