【问题标题】:How I can filter a DataTable with DateTime Columns?如何过滤带有 DateTime 列的 DataTable?
【发布时间】:2013-01-24 10:03:24
【问题描述】:

我想过滤一个 Datatable 并且它可以工作,但是如果我搜索一个 DateTime 我会得到一个错误。

这是我的代码。我做错了什么?

 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.

我该怎么做?

【问题讨论】:

  • 尝试在从数据库中获取数据时将日期和时间转换为字符串......
  • 为什么我们都使用相同的SearchString
  • 我用 mysql 适配器做的

标签: c# listview datatable datarow


【解决方案1】:

试试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

【讨论】:

    猜你喜欢
    • 2014-04-23
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    相关资源
    最近更新 更多