当前位置:首页 > 数据库 > SQlite

SQLite bigint至C#中的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;
                        }

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

解决方法:

这是一种存储为DateTime而不是bigint的解决方案:

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

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


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!