2013-12-11 70 views
0

我有一個GridView將有多個搜索選項。變量搜索查詢

Call Date 
Start_Time (between two time values) 
Call_Time 
AgentID 
Phone 

他們想要的是所有這些選項都可以通過單個搜索按鈕進行搜索。

我遇到的問題是如何建立查詢是動態的,所以如果他們只搜索一個選項,它將搜索,如果他們通過上述三個搜索它仍然會提供準確的結果。

這裏是查詢至今:

 SELECT *, 'file://///server/folder/' + replace(call_date, '/', '') + '/' 
+ qa_status + '.vox' as url FROM [JM_NSC_Recordings] WHERE ([areacode] + [phone] = @phone) 

Phone = [areacode] + [phone] // @phone 
Start Time = between [start_time] and [start_time] @starttime 
Call Time = [call_time] // @calltime 
AgentID = [tsr] // @agentid 
Call Date = [call_date] // @calldate 

在數據庫中這些字段的所有都是VARCHAR

回答

0

我用同一類型的高級搜索中有多個參數的一個StringBuilder。

StringBuilder stmt=new StringBuilder(); 
stmt.AppendLine("select * from table where "); 

if(phone not blank) 
stmt.AppendLine("phone=89754654 and "); 
else 
stmt.AppendLine("true and "); 



if(agentid not blank) 
    stmt.AppendLine("agentid=89754654 and"); 
    else 
    stmt.AppendLine("true and "); 

else 
    stmt.AppendLine("true "); 

等嘗試將工作

+0

當時tryign避免做大量的工作在後面的代碼。能夠找到一個基於SQL的解決方案'WHERE(@phone爲null或[areacode] + [phone] = @phone)' –