【问题标题】:Comparing C# datetime value to SQL Server Compact 4 datetime将 C# 日期时间值与 SQL Server Compact 4 日期时间进行比较
【发布时间】:2013-06-28 02:02:52
【问题描述】:

我正在构建一个存储一组日期时间的应用程序,以跟踪我打印特定报告的时间。该报告由第二个表中的信息组成,该表也有日期时间。我正在尝试让报告使用仅在第一个表中的最后一个日期时间之后的记录填充 datagridview。

第一个表称为'deliverylog',该表存储过去的打印日期。第二个表称为“joblog”,它存储以前的作业条目的记录。

当我运行该程序时,它工作得很好,并用最后一个日期之后的所有记录填充了网格视图,但它没有被完善......它只填充日期之后的日期而不是时间。我需要查询将gridview填充到第二个......

DateTime lastDeliveryDate;

private void getLastDelivery() // Sets global variable lastDeliveryDate to the last timestamp entered in the deliverylog table 
{
    openLogConnection();

    try
    {
        command = new SqlCeCommand("SELECT TOP 1 * FROM deliverylog ORDER BY Id DESC", logConn);

        drLogSet = command.ExecuteReader();
        while (drLogSet.Read())
        {
            lastDeliveryDate = Convert.ToDateTime(drLogSet["Timestamp"]);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        logConn.Close();
    }
}

private void populateGridView()
{
    openLogConnection();

    try
    {
        command = new SqlCeCommand("SELECT * FROM joblog WHERE TimeStamp > @date", logConn);
        command.Parameters.AddWithValue("@date", lastDeliveryDate);

        dtLogSet = new DataTable();
        bsLogSet = new BindingSource();
        daLogSet = new SqlCeDataAdapter(command);
        cbLogSet = new SqlCeCommandBuilder(daLogSet);

        daLogSet.Fill(dtLogSet);
        bsLogSet.DataSource = dtLogSet;
        dataGridView1.DataSource = bsLogSet;

        dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        logConn.Close();
    }
}

有人知道如何让这个工作正常吗?我将两个表的时间戳存储为日期时间数据类型,格式如下:“MM/dd/yyyy hh:mm:ss tt”

【问题讨论】:

  • 能否请您发布joblog 表定义?
  • 你真的在使用 SQLite 或 SQL Serve Compact Edition 吗?
  • sql server compact 4.0
  • 好的,将更改标签和标题以反映这一事实。
  • 除非您的 TimeStamp 字段是 varchar,否则您实际上不会以 any 字符串格式存储时间戳,这很好。你能验证你的TimeStamp字段的类型吗?

标签: c# datetime datagridview sql-server-ce-4 datetime-comparison


【解决方案1】:

问题在于可能这一行:

lastDeliveryDate = Convert.ToDateTime(drLogSet["Timestamp"]);

假设您的 TimeStamp 字段是 datetime 类型,您应该使用:

lastDeliveryDate = (DateTime) drLogSet["TimeStamp"];

请确认您的TimeStamp字段的数据类型。

【讨论】:

    【解决方案2】:

    我相信使用AddWithValue 方法会在内部转换值,并且可能会失去所需的时间精度。而是使用Parameters 集合的Add(String, SqlDbType) 方法重载:

    var dateParameter = command.Parameters.Add("@date", SqlDbType.DateTime);
    dateParameter.Value = this.lastDeliveryDate;
    

    通过使用调试器检查变量,确保在设置参数值之前具有正确的值。

    您可以尝试使用DATEDIFF SQL 函数而不是> 运算符来确保正确的精度:

    SELECT * FROM joblog WHERE DATEDIFF(second, @date, TimeStamp) > 0
    

    【讨论】:

    • 实际上 add() 方法已被弃用......我让它工作了,这是我首先存储日期时间值的方式,我使用标准时间并尝试阅读它是军事时间......哈哈,这将是一个愚蠢的错误......
    • Add(String, Object) 方法重载已被弃用并替换为 AddWithValue(String, Object)Add(String, SqlDbType) 方法重载未被弃用。不要感觉不好数据格式差异是一个很常见的错误,这就是为什么我建议在调试时检查参数值。
    【解决方案3】:

    您应该将日期格式化为 yyyyMMdd hh:mm:ss。

    lastDeliveryDate.toString("yyyyMMdd hh:mm:ss").

    在这篇文章中有更多细节。 How to compare sqlite TIMESTAMP values

    【讨论】:

    • 他没有运行SQLite,字符串格式是用来显示和解析的,不是用来比较的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 2014-05-03
    • 2013-09-16
    • 1970-01-01
    相关资源
    最近更新 更多