C#C
C#4y ago
Nemesis

How to read DateTime from SQLite database using System.Data.SQLite?

I have a sqlite database with a DateTime entry. Since SQLite doesn't have a DateTime data type, it stores it as Int64 time ticks. I am able to read the correct value of the column when using any SQLite database parser available online.
But when I try to read it in C#, using this code:
var cmdReadDuration = sqliteConnection.CreateCommand();
cmdReadDuration.Transaction = sqliteConnection.BeginTransaction();
cmdReadDuration.CommandText = @"SELECT * FROM Duration";
cmdReadDuration.Prepare();
var sqlReader = cmdReadDuration.ExecuteReader();
while (sqlReader.Read())
{
    var time = sqlReader["Time"];
}

I only get 2022, the year part in the variable.
What am I doing wrong?
Was this page helpful?