SQLite bigint到C#中的日期时间

时间:2017-05-09 02:21:28

标签: c# sqlite datetime

我在我的UWP项目中使用SQLite。 我的模型类有DateTime字段:

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

}

问题是SQLite将DateTime字段创建为bigint。如何阅读此bigint并设置为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;
                        }

上面的代码没有返回错误,但日期值错误。 `

1 个答案:

答案 0 :(得分:3)

以下是以DateTime而不是bigint 存储的解决方案:

SQLiteConnection构造函数采用storeDateTimeAsTicks参数,默认为true。如果您希望它使用DateTime类型而不是bigint,则可以将其传递为false。

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