Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.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
C# 在预期条件的上下文中指定的非布尔型表达式,靠近'@val4';_C#_Sql_Sql Server_Parameters_Sql Server 2012 - Fatal编程技术网

C# 在预期条件的上下文中指定的非布尔型表达式,靠近'@val4';

C# 在预期条件的上下文中指定的非布尔型表达式,靠近'@val4';,c#,sql,sql-server,parameters,sql-server-2012,C#,Sql,Sql Server,Parameters,Sql Server 2012,嘿,我不知道为什么会出现这个错误: 在“@val4”附近预期条件的上下文中指定的非布尔类型表达式 当我尝试在C#中运行此代码时: 上述查询的数据为: @val1 = 1 @val2 = 9/30/2016 12:00:00 AM @val3 = 9/30/2017 12:00:00 AM @val4 = (RequestID = 0 OR RequestID =469 OR RequestID =471 OR RequestID =472 OR RequestID =4

嘿,我不知道为什么会出现这个错误:

在“@val4”附近预期条件的上下文中指定的非布尔类型表达式

当我尝试在C#中运行此代码时:

上述查询的数据为:

@val1 = 1
@val2 = 9/30/2016 12:00:00 AM
@val3 = 9/30/2017 12:00:00 AM
@val4 =  (RequestID = 0  OR RequestID =469 OR RequestID =471 OR RequestID =472 OR 
          RequestID =473 OR RequestID =474 OR RequestID =494 OR 
          RequestID =496 OR RequestID =497)
总之,看起来是这样的:

SELECT * 
FROM tT 
WHERE active = 1 
AND (ApDate BETWEEN '9/30/2016 12:00:00 AM' AND '9/30/2017 12:00:00 AM') 
AND (RequestID = 0  OR RequestID =469 OR RequestID =471 OR RequestID =472 OR 
     RequestID =473 OR RequestID =474 OR RequestID =494 OR 
     RequestID =496 OR RequestID =497)
但是,当我在ServerManagementStudio中运行相同的查询时,它运行得很好

我只是用这个:

command.Parameters.AddWithValue("@val1", Value_here);
command.Parameters.AddWithValue("@val2", Value_here);
command.Parameters.AddWithValue("@val3", Value_here);
command.Parameters.AddWithValue("@val4", Value_here);
作为向查询中添加4个参数的方法


注意当前命名的问题不是一个dup,因为它们不会对当前问题进行重复。

您不能将SQL语句放入SQL语句中的参数中

SQL Server将无法识别参数中的SQL,因为它需要一个值。

您可以使用和存储过程来执行此操作

首先,您需要创建一个表类型以用于以下过程:

create type dbo.Id_udt as table (Id int not null);
go
create procedure dbo.get_requests (
    @IsActive bit
  , @FromDate datetime
  , @ThruDate datetime
  , @Ids as dbo.Id_udt readonly
) as 
begin;
  set nocount, xact_abort on;

  select * 
  from t 
    inner join @Ids as i
      on t.RequestID = i.RequestID
  where t.active = @IsActive
    and t.apdate between @FromDate and @ThruDate

end;
go
然后,创建一个过程:

create type dbo.Id_udt as table (Id int not null);
go
create procedure dbo.get_requests (
    @IsActive bit
  , @FromDate datetime
  , @ThruDate datetime
  , @Ids as dbo.Id_udt readonly
) as 
begin;
  set nocount, xact_abort on;

  select * 
  from t 
    inner join @Ids as i
      on t.RequestID = i.RequestID
  where t.active = @IsActive
    and t.apdate between @FromDate and @ThruDate

end;
go
然后,使用
DataTable
requestid的列表组装并传递给存储过程,DataTable使用
SqlDbType.Structured
作为
SqlParameter
添加

表值参数参考:


@DavidG正在进行比较。。它被比作请求,令人担忧,误读。但您似乎试图将原始SQL作为参数传入,对吗?你能告诉我你是怎么做的吗?现在你有答案了,但我建议你不能把逻辑作为参数传递。参数是值,而不是语句。由于您使用的是即席查询,因此您应该明确使用您的数据类型。它们并不总是被正确地解释@SeanRange如果
AddWithValue
在.Net核心中,你知道吗?还没看,但我希望它被烧掉了!所以我只需要将其作为普通字符串传递?我强烈警告您不要构建这样的字符串,因为它很容易被sql注入。@SeanLange好吧,只有当用户能够以某种方式操纵string@Istanfin这在很多情况下都很容易做到,这就是为什么SQL注入仍然是最大的安全问题。