2013-01-24 113 views
1

我想過濾一個Datatable,它的工作原理,但如果我搜索DateTime我得到一個錯誤。如何使用DateTime列過濾DataTable?

這是我的代碼。我做了什麼錯事?

DataTable tb = DataBaseManager.GetRadiusDataTable(radiusconnectionstring, "marksullivan"); 

DataRow[] filteredRows = tb.Select("AcctStartTime LIKE '%" + searchstring + "%' OR AcctStopTime LIKE '%" + searchstring + "%' OR FramedIPAddress LIKE '%" + searchstring + "%'"); 
tb = filteredRows.CopyToDataTable(); 
this.ListView.DataSource = tb; 
this.ListView.DataBind(); 

AcctStartTime:日期時間 AcctStopTime:日期時間 FramedIPAddress:VARCHAR

The error: The Operation 'Like' could not to System.DateTime and System.String execute. 

我怎樣才能做到這一點?

+0

嘗試將日期和時間轉換爲字符串時,當你從數據庫獲取數據的時間.... –

+0

爲什麼我們使用的是相同的'SearchString'所有? – Kiran1016

+0

我使用一個mysql適配器 – Tarasov

回答

7

嘗試這樣DateComparision in RowFilter

string filter = "DateFrom > '" + daDateFrom + "' AND DateTo <= '" + daDateTo + "'"; 
tb.Select(filter) 

或 從DataRow filter Examples

日期值的鮮明字符##內包圍。日期格式 與 不變或英文文化的DateTime.ToString()方法的結果相同。

[C#]

dataView.RowFilter = "Date = #12/31/2008#"   // date value (time is 00:00:00) 
dataView.RowFilter = "Date = #2008-12-31#"   // also this format is supported 
dataView.RowFilter = "Date = #12/31/2008 16:44:58#" // date and time value