删除SQL查询中的Where子句中的条件
问题描述:
我有一个要求,我想要删除SQL查询字符串中'where'子句中的条件。删除SQL查询中的Where子句中的条件
这去除应该像这样工作,如果我的查询是
Select *
from table1
where column1 = @c1 and column2 = 10
然后取出后应该
Select *
from table1
where column2 = 10
如果我的SQL查询是
Select *
from table1
where column1 = @c1
再经过去除它应该是
Select *
from table1
在C#中执行此操作的最佳方法是什么?我可以使用正则表达式还是有更好的方法?
答
这不是最漂亮的方式,但这会做你的情况。
return query.Replace((query.IndexOf(" and ") != -1 ? "column1 = @c1 and " : " where column1 = @c1"), string.Empty);
以相反的方式去做; 'where(column1 = @ c1或@ c1为空)和(column2 = 10)'。 – jarlh
所以每次你想删除'where column1 = @ c1'? – WhatsThePoint
如果“是”是@WhatsThePoint问题的答案:当多于一个的时候它总是第一个发言的地方? –