【问题标题】:How to split queries in different condition and check which condition is failing the query in c#如何在不同条件下拆分查询并检查 C# 中哪个条件使查询失败
【发布时间】:2018-04-29 05:13:57
【问题描述】:

我正在编写一个代码,如果我们在其中进行查询,它将显示查询的哪个 where 子句没有在 c# 中获取任何数据。

查询结构类似于

select * from ABD where a=0 and b= 42 and c=(Select c from azs) or f=89 or x=10

【问题讨论】:

  • 查询的哪个“where”子句是什么意思WHERE 子句。
  • 不多次执行查询是不可能的。如果多次执行没问题,那么你可以在每个Where()之后执行Any()
  • 之后有多个条件要检查。我需要验证哪个条件使查询失败。我们可以多次执行sql查询
  • 可能是多个部分 结合 ,这会有所不同。您的程序将需要对什么是什么以及如何使用它有所了解。
  • select * from ABD where a=0 and b= 42 and (c=(Select c from azs) or f=89 or x=10) 用户括号用于 OR 条件使其成为一个块,我同意@mshsayem,检查每个 where 条件的 any()

标签: c# sql split


【解决方案1】:

如果您正在使用已知查询,您可以编写查询以指示 where 条件的哪些术语将为每一行返回 true 或 false,如下所示:

select
  case when a=0 then 1 else 0 end as 'term1',
  case when b=42 then 1 else 0 end as 'term2',
  case when c=(select c from azs) then 1 else 0 end as 'term3',
  case when f=89 then 1 else 0 end as 'term4',
  case when x=10 then 1 else 0 end as 'term5'
from ABD

如果您的问题更为普遍,因为您事先不知道实际的查询,那么您将需要编写一个程序来将输入查询解析为各个术语。这可以通过使用 string index-of 和 split 以及类似的方法来定位单个术语来完成简单的场景。或者可以通过实现一个简单的 SQL 解析器来完成。我建议Irony 是一个优秀的 C# 解析器框架,它甚至包括一个SQL sample grammar

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多