C#TableAdapter可选WHERE子句

时间:2014-08-23 13:58:22

标签: c# sql datatable

我在QueryBuilder中有这样的查询:

SELECT Items    FROM     Tables
WHERE  (Transactions.transaction_type = ? AND
Items.designation LIKE '%'+?+'%' AND Transactions.transaction_date >= ?
AND Transactions.transaction_date <= ? AND Items.item_number = ?)

后来使用它是这样的:

this.TransactionItemsReportTableAdapter.Fill(this.InventoryDataSet.TransactionItemsReport, 10,
"", DateTime.MinValue, DateTime.MaxValue, 0);

但是我并不总是希望按item_number进行过滤,因此:是否可以暂时忽略WHERE子句或设置空值(如在指定参数中)而无需在设计器中创建单独的查询?

1 个答案:

答案 0 :(得分:0)

你走了:

SELECT Items FROM Tables
WHERE Transactions.transaction_type = @TransatctionType AND
Items.designation LIKE '%'+ @Designation +'%'
AND Transactions.transaction_date >= @DateFrom
AND Transactions.transaction_date <= @DateTo 
AND (@ItemNumber IS NULL OR Items.item_number = @ItemNumber)

不创建新查询的原因是什么?在这种情况下,代码更易于维护。