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

时间:2014-11-03 11:15:09

标签: c# sqlite windows-phone-8 windows-runtime

如何使用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数据库以及如何阅读?

1 个答案:

答案 0 :(得分:1)

以下是我的代码示例:

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

    }