简体   繁体   中英

Comparing C# datetime value to SQL Server Compact 4 datetime

I'm building an application that stores a group of datetimes to keep track of when I print a particular report. The report is built up of information from a second table that also has datetimes. I'm trying to get the report to populate a datagridview with records that are only after the last datetime in the first table.

The first table is called 'deliverylog', this table stores the past print dates. The second table is called 'joblog', and it stores the records of previous job entries.

When I run the program, it works just fine and populates the gridview with all records after the last date, but it's not refined... it only populates with dates after the date and not the time. I need the query to populate the gridview to the second....

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();
    }
}

Anyone know how to get the this working right? I'm storing the timestamps for both tables as datetime data types and in the following format: "MM/dd/yyyy hh:mm:ss tt"

I believe that using the AddWithValue method is internally converting the value and probably loosing the desired time precision. Instead, use the Add(String, SqlDbType) method overload of the Parameters collection:

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

Make sure you have the correct value before setting the parameter value by inspecting the variable using the debugger.

And you could try using the DATEDIFF SQL function instead of the > operator to ensure proper precision:

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

The problems is probably this line:

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

Assuming your TimeStamp field is a datetime type, you should use:

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

Please confirm the data type of your TimeStamp field.

You should format the date to yyyyMMdd hh:mm:ss.

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

In this postare more details. How to compare sqlite TIMESTAMP values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM