Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite bigint to Datetime in C#

I'm using SQLite in my UWP project. My model class has DateTime fields:

public class EducationInfo
{
    [SQLite.Net.Attributes.PrimaryKey, SQLite.Net.Attributes.AutoIncrement]
    public int educationInfoId { get; set; }
    public String Education { get; set; }
    public bool Enabled { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int UserId { get; set; }

    // Not in the DB
    [Ignore]
    public string FormattedStartDate { get; set; }
    [Ignore]
    public string FormattedEndDate { get; set; }

    public EducationInfo()
    {}

    public EducationInfo(String edu, bool enabled, DateTime st, DateTime ed, int userId)
    {
        this.Education = edu;
        this.Enabled = enabled;
        this.StartDate = st;
        this.EndDate = ed;
        this.UserId = userId;
    }

}

The problem is that SQLite create the DateTime fields as bigint. How can I read this bigint and set as DateTime?

`

var education = connection.Table<EducationInfo>().Where(e => e.UserId == user.userID);
                        foreach(var edu in education)
                        {
                            EducationInfo educationInfo = new EducationInfo();
                            educationInfo.StartDate = edu.StartDate;
                        }

The code above doesn't return error, but the date value is wrong. `

like image 256
user6824563 Avatar asked Sep 15 '25 10:09

user6824563


1 Answers

Here is a solution to store as DateTime instead of bigint:

The SQLiteConnection constructor takes a parameter storeDateTimeAsTicks which defaults to true. You can pass that as false if you want it to use a DateTime type instead of bigint.

https://github.com/oysteinkrog/SQLite.Net-PCL/issues/203

like image 82
sachin Avatar answered Sep 18 '25 10:09

sachin