在C#中使用SQLite数据库与SQL数据库相识,但不完全相同。
例如在wInform中要判断表中是否存在某条数据
SQL数据库方法
con.Open();
SqlCommand cmd = new SqlCommand(NameStr, con);
int result = Convert.ToInt32(cmd.ExecuteScalar());
if(result)
{
//存在
}
else
{
//不纯在
}
SQLite数据库方法
//判断数据是否存在
var Places = con.Table<Place>().Where(Place => Place.Id.Equals(Id)).FirstOrDefault();
if (Places == null)
{
//插入新数据
var List = new List<object>();
List.Add(new Place() {Id = Id, x = x, y = y, hegiht = hegiht, width = width });
con.InsertAll(List);
Console.WriteLine("插入完成");
}
else
{
//更新已有数据
Places.x = x;
Places.y = y;
Places.width = width;
Places.hegiht = hegiht;
con.Update(Places);
Console.WriteLine("更新完成");
}
注意:这里一定要使用FirstOrDefault(),如果直接写con.Table().Where()会报错!因为系统不知道数据是否唯一