繁体   English   中英

如何使用C#将自定义对象保存到Windows Phone的sqlite中?

[英]How to save custom object into sqlite on windows phone using c#?

如何使用C#将自定义对象保存到Windows Phone的sqlite中?

 public class Person
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    [MaxLength(30)]
    public string Name { get; set; }

    [MaxLength(30)]
    public string Surname { get; set; }

    public List<Address> Items { get; set; }

}
public class Address
{
    public string city { get; set; }
    public string state { get; set; }
    public string pin { get; set; }
}

如何将Person插入sqlite数据库以及如何阅读?

这是我的代码示例:

    private const string db_name = "store4.sqlite";
    SQLiteAsyncConnection connection = new SQLiteAsyncConnection(db_name);


    public async void InsertInspections(ObservableCollection<Inspections> inspections)
    {
        connection.CreateTableAsync<Inspections>().Wait();

        foreach (var inspection in inspections)
        {
            var task = connection.Table<Inspections>().
                Where(v => v.InspectionId == inspection.InspectionId).ToListAsync();
            task.Wait();

            if (task.Result.Any())
                connection.UpdateAsync(inspection);
            else
                connection.InsertAsync(inspection);
        }

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM