数据表选择功能

时间:2011-12-26 13:46:27

标签: c# datatable

尝试选择“预期关闭日期”小于今天日期的字段,并从表中删除它们。但是在'closure'操作符之后得到像Missing操作数这样的错误。

DateTime Tdy=DateTime.Now;
 var rows = dt.Select("Expected closure date < "+Tdy+"");
 foreach (var row in rows)
         row.Delete(); 

3 个答案:

答案 0 :(得分:1)

DateTime Tdy=DateTime.Now;
 var rows = dt.Select("myDateTimecolumn<'"+DateTime.Now.ToString("yyyyMMdd")+"'");
 foreach (var row in rows)
         row.Delete();

答案 1 :(得分:1)

你需要用octothorpes包装日期:

dt.Select("myDateTimecolumn < #" + tdy.ToString("MM/dd/yyyy") + "#");

答案 2 :(得分:1)

这就是你需要的,经过测试

var rows = dt.Select(string.Format("[Expected closure date] < '{0}'", Tdy));

这是完整的工作样本

using System;
using System.Data;


namespace ConsoleApplication5
{
  class Program
  {    

    static void Main(string[] args)
    {
      DataTable dt = new DataTable();
      string col = "Expected closure date";
      dt.Columns.Add(col,typeof(DateTime));

      dt.Rows.Add(DateTime.Now.AddDays(-1) );

      Console.WriteLine("Total rows in dt " + dt.Rows.Count);

      DateTime Tdy = DateTime.Now;
      var rows = dt.Select(string.Format("[Expected closure date] <= '{0}'", Tdy));
      foreach (var row in rows)
        row.Delete();
      Console.WriteLine("Total rows in dt " + dt.Rows.Count);

      Console.ReadLine();

    }
  }
}

这是输出

Total rows in dt 1
Total rows in dt 0