简体   繁体   English

(在参数中传递“all”?)| 我如何制作参数? 在 WHERE 过滤器中返回 ALL 在 sql 请求中

[英](pass "all" in parameter ?) | how can I make parameter ? in WHERE filter to be a return of ALL in sql request

I am making a wpf C# app following MVVM.我正在按照 MVVM 制作一个 wpf C# 应用程序。 It processes data from accdb (with several connected tables) using DataSet via ServerExplorer.它通过 ServerExplorer 使用 DataSet 处理来自 accdb(带有多个连接的表)的数据。 I am trying to make a filtered search right now.我现在正在尝试进行过滤搜索。

        SELECT        IDBug, [Date], ReportedBy, Replicated, ReproducableSteps, AppArea, Status, FixedVer, FixedBy, Notes, Title
        FROM            Bugs
        WHERE      ([Date] >=  ?)
        AND ([Date] <= ?) 
        AND (Status = ?)
        AND (AppArea = ?)
        AND (Replicated = ?)
        AND (ReportedBy =  ?)

This was working, but only if use all the filter settings.这是有效的,但前提是使用所有过滤器设置。

        int idAr = (from DataRow dr in ars.GetData().Rows
                                        where (string)dr["Area"] == AreaSe.ToString()
                                        select (int)dr["IDArea"]).FirstOrDefault();
        

        int idCust = (from DataRow dr in custs.GetData().Rows
                                          where (string)dr["CustomerName"] == CustomS.ToString()
                                          select (int)dr["IDCustomer"]).FirstOrDefault();


        BugTable = bugs.GetT(StartDate, StopDate, State, idAr, replicationS, idCust);

I do have some null values in my data, that is why I tried to do smth similar to我的数据中确实有一些空值,这就是为什么我试图做类似于

WHERE Title = ? WHERE 标题 = ? OR (? IS NULL))或 (? 是 NULL))

instead of代替

WHERE Title = ? WHERE 标题 = ?

for each of the parameters.对于每个参数。 But it messes up the data types...但它弄乱了数据类型......

I don`t think that is smart to write a different sql query for each case, so I need to know how cann I pass "SELECT ALL" to a ?我不认为为每种情况编写不同的 sql 查询是明智的,所以我需要知道如何将“SELECT ALL”传递给 ? parameter.范围。

You can do use case in where clause like this.您可以像这样在where子句中使用用case You can pass null as default value for the parameter.您可以将null作为参数的默认值传递。

So it will return the records for @param if it is not passed else it will return all records.因此,如果未通过,它将返回@param的记录,否则它将返回所有记录。

SELECT Col1, Col2….
FROM TableName 
WHERE ColName = CASE WHEN @param IS NULL THEN ColName ELSE @param END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM