【问题标题】:DataTable select method with Dates not showing right result日期未显示正确结果的 DataTable 选择方法
【发布时间】:2014-11-18 09:48:40
【问题描述】:

我有一个数据表,其中包含我需要删除并插入另一个数据表的行。我正在选择列 SLA dd-MM-yyyy HH:mm。

这是我的代码:

public SLAWarningHandler()
{
}
public DataTable SLAWarningData(DataTable queryData)
{
    DataTable data = queryData;
    try
    {
        DateTime date = DateTime.Now;
        DataRow[] rows = data.Select("SLA < '#" + date + "#'");
        DataTable actual = new DataTable();
        actual = data.Clone();
        foreach (DataRow row in rows)
        {
            actual.ImportRow(row);
        }
    }
    catch (Exception exception)
    {
        throw exception;
    }
    return data;
}

我尝试将日期参数转换为这样的字符串:

date.ToString("dd-MM-yyyy HH:mm")

但这没有帮助。

谁能指出我的问题?

谢谢!

【问题讨论】:

  • 您的数据提供商是什么?
  • @SonerGönül 数据提供者是 Ole DB
  • @sdeep 我一直在查看您发送的链接,但您看不出我的代码与他的代码有何不同。

标签: c# .net jquery-datatables


【解决方案1】:

你的问题是你返回错误的东西。您必须返回实际,但您返回的数据是原始记录集。

public DataTable SLAWarningData(DataTable queryData)

    {
        DataTable actual = null;
        DataTable data = queryData;
        try
        {
            DateTime date = DateTime.Now;
            DataRow[] rows = data.Select("SLA < '#" + date + "#'");

            actual = data.Clone();
            foreach (DataRow row in rows)
            {
                actual.ImportRow(row);
            }
        }
        catch (Exception exception)
        {
            throw exception;
        }
        return actual;
    }

【讨论】:

  • 你也不能在克隆原始数据时创建空的DataTable:DataTable actual = null;
【解决方案2】:

使用标准 ISO 格式 (yyyyMMdd hh:mm:ss):

DataRow[] rows = data.Select(string.Format("SLA < '{0}'", date.ToString("yyyyMMdd hh:mm:ss"));

或者:

DataRow[] rows = data.Select(string.Format("SLA < '{0}'", date.ToString(DateTimeFormatInfo.InvariantInfo));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多