【问题标题】:parameterized query issue with multiple conditions [closed]具有多个条件的参数化查询问题[关闭]
【发布时间】:2021-08-26 08:40:40
【问题描述】:

我正在尝试创建具有多个条件的参数化 sql 查询。我在使用模块 System.Data.Common.DbCommand

的实体框架版本 2.2 和 MSSQL 2016 上打开一个命令

当我将 @tenantName 之后的 AND 更改为 OR 时,sql 命令起作用。我需要它是一个 AND 语句才能得到正确的数字。

以下 sql 命令代码无效,但在 sql management studio 中有效

var command = _context.Database.GetDbConnection().CreateCommand();
command.Transaction = _context.Database.CurrentTransaction.GetDbTransaction();

command.Parameters.Add(new SqlParameter("@tenantName", tenantName));
command.Parameters.Add(new SqlParameter("@searchTerm", searchTerm));
command.Parameters.Add(new SqlParameter("@hashedSearchTerm", hashedSearchTerm));

command.CommandText = "SELECT COUNT(*) FROM dbo.IdpUserEventLog WHERE " +
"(TenantName IS NOT NULL AND TenantName = @tenantName) AND (" +
"(EventType IS NOT NULL AND EventType LIKE '%@searchTerm%') " +
"OR (AppName IS NOT NULL AND AppName LIKE '%@searchTerm%') " +
"OR (EventExtra IS NOT NULL AND EventExtra LIKE '%@searchTerm%') " +
"OR (EventDescription IS NOT NULL AND EventDescription LIKE '%@searchTerm%') " +
"OR (EventResultReport IS NOT NULL AND EventResultReport LIKE '%@searchTerm%') " +
"OR (EventDescription IS NOT NULL AND EventDescription LIKE '%@hashedSearchTerm%') " +
"OR (EventExtra IS NOT NULL AND EventExtra LIKE '%@hashedSearchTerm%')) " +

【问题讨论】:

  • “不工作”是什么意思。仅将 AND 替换为 OR 不会使查询功能或功能失调。查询仍将执行,但结果可能不如预期。但是,如果没有任何示例数据,这是不可能的……
  • 您的查询格式错误。查看 MSDN 中的此示例以使用实体框架构建您的查询:docs.microsoft.com/es-es/dotnet/framework/data/adonet/ef/…
  • TenantName IS NOT NULL AND 可以被删除,因为它什么都不做。大多数空检查都是一样的。
  • '%@hashedSearchTerm%' 应该是 @hashedSearchTerm% 应该包含在 hashedSearchTerm 的值中(在 C# 中,而不是在 SQL 中)。其他喜欢的也一样。
  • 您的情况可能是“短路评估”。即你的WHERE 子句的第二部分(即AND 之后的任何内容)永远不会评估为真(因为@mjwills 提到了错误的like 条件),因此condition1 AND condition2 也将是假的。但是当您使用condition1 OR condition2 时,第二部分根本不会被计算(因为如果条件1 已经为真,则整个表达式变为真,而不管条件2 的结果如何)

标签: c# sql sql-server sql-server-2016


【解决方案1】:

感谢@mjwills 和@derpirscher 的评论,现在通过将代码更改为以下代码,我得到了与在 sql management studio 中相同的结果

var command = _context.Database.GetDbConnection().CreateCommand();
command.Transaction = _context.Database.CurrentTransaction.GetDbTransaction();

command.Parameters.Add(new SqlParameter("@tenantName", tenantName));
command.Parameters.Add(new SqlParameter("@searchTerm", $"%{searchTerm}%"));
command.Parameters.Add(new SqlParameter("@hashedSearchTerm", $"%{hashedSearchTerm}%"));

command.CommandText = "SELECT COUNT(*) FROM dbo.IdpUserEventLog WHERE " +
"(TenantName = @tenantName) AND (" +
"(EventType LIKE @searchTerm) " +
"OR (AppName LIKE @searchTerm) " +
"OR (EventExtra LIKE @searchTerm) " +
"OR (EventDescription LIKE @searchTerm) " +
"OR (EventResultReport LIKE @searchTerm) " +
"OR (EventDescription LIKE @hashedSearchTerm) " +
"OR (EventExtra LIKE @hashedSearchTerm)) " +

【讨论】:

  • 很高兴听到它现在正在工作。不要忘记将您的问题标记为已回答以关闭它。 ?
  • 当我点击复选标记时,我收到一条错误消息,提示我可以在 2 天内接受我自己的答案
猜你喜欢
  • 2013-01-04
  • 2011-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-06
  • 1970-01-01
  • 2017-02-24
  • 1970-01-01
相关资源
最近更新 更多