C# 如何使用日期时间列过滤数据表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14498559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How I can filter a DataTable with DateTime Columns?
提问by Tarasov
I want to filter a Datatable and it works but if I search a DateTime I get a error.
我想过滤一个数据表并且它可以工作,但是如果我搜索一个 DateTime,我会收到一个错误。
Here is my code. What wrong have I done?
这是我的代码。我做错了什么?
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:datetime AcctStopTime :datetime FramedIPAddress : varchar
AcctStartTime:datetime AcctStopTime:datetime FramedIPAddress:varchar
The error: The Operation 'Like' could not to System.DateTime and System.String execute.
How can I do this?
我怎样才能做到这一点?
回答by Ravi Gadag
try like this DateComparision in RowFilter
在 RowFilter 中尝试这样的DateComparision
string filter = "DateFrom > '" + daDateFrom + "' AND DateTo <= '" + daDateTo + "'";
tb.Select(filter)
or from DataRow filter Examples
Date values are enclosed within sharp characters # #. The date format is the same as is the result of DateTime.ToString() method for invariant or English culture.
日期值用尖字符## 括起来。日期格式与 DateTime.ToString() 方法对于不变或英语区域性的结果相同。
[C#]
[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