Sqlite, check the exact difference between a date, time and minutes of the current date [duplicate]

0

I need to do a check between two dates, I need to check if the date, time and minutes of a field is greater than or equal to the current date, time and minutes, I am using

If this condition is true, the data goes to datagridview_2 otherwise it remains in datagridview_1 .

In short: The datagridview_2 will only receive information if the current date is greater than or equal to the date of the "end data" column. This data Final , the user you specify in the field.

    
asked by anonymous 18.07.2018 / 23:10

1 answer

1

SQLite does not have a default type for storing date and time. You can find more information here: link

However, built-in functions allow you to store this type of data using TEXT, REAL or INTEGER . For this answer I will use the most common data format for storing dates in SQLite - TEXT .

To check between two dates, you can submit a parameter in your query using the AddWithValue () method. This will prevent SQLInjection. In the example below, I make use of the ISO8601 standard which is an international standard for storing date and time. You can find more information about this here:

link

Follow the code.

        using (SQLiteConnection connection = new SQLiteConnection(@"sua_conexao_aqui"))
        {
            string consulta = "SELECT * FROM info WHERE dataFinal > @dataFinal";

            DateTime dataFinal = DateTime.Now.AddMonths(-6);

            using (SQLiteCommand commandReader = new SQLiteCommand(consulta, connection))
            {
                commandReader.Parameters.AddWithValue("@dataFinal", dataFinal.ToString("o")); //ISO8601
                SQLiteDataReader reader = commandReader.ExecuteReader();

                while (reader.Read())
                {
                    DateTime suaData = Convert.ToDateTime(reader[0]);
                }
            }
        }
    
19.07.2018 / 14:01