如何使用c#将自定义对象保存到windowsphone上的sqlite中
本文关键字:windowsphone 上的 sqlite 保存 对象 何使用 自定义 | 更新日期: 2023-09-27 18:28:50
如何使用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);
}
}