Windows 8 Metro用C#连接SQLite及创建数据库,数据表的增删改查的实现

时间:2021-05-20

  1.Metro中使用SQLite数据库具体步骤如下:

  1).下载SQLite for WinRT

  地址:http://bine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Member.sqlite"); //数据文件保存的位置

      using (var db = new SQLite.SQLiteConnection(path)) //打开创建数据库和表

 {

        db.CreateTable<MemberInfo>();

}

}



  3).简单的操作sqlite数据库(增,删,改,查询)
复制代码 代码如下:
  public void Insert(MemberInfo data)

{

    try

{

       using (var db = newSQLiteConnection(path))

      {

   db.Insert(data);

}

}

    catch(Exception e)

{

      throw e;

}

}

  publicvoid Delete(int id)

{

    try

  {

   T data = Select(id);

      using (var db = newSQLiteConnection(path))

   {

  db.Delete(data);

   }

}

    catch(Exception e)

{

       throw e;

}

}

   public void Insert(T data)

{

     try

 {

        using (var db = newSQLiteConnection(path))

       {

    db.Insert(data);

}

}

     catch(Exception e)

{

       throw e;

}

}

    publicvoid Delete(int id)

 {

     try

{

   T data = Select(id);

       using (var db = newSQLiteConnection(path))

  {

   db.Delete(data);

}

}

     catch(Exception e)

{

      throw e;

}

}

  public MemberInfo Select(int id)

{

    try

     {

        MemberInfo data = null;

        using (var db = newSQLiteConnection(path))

       {

          List<object> obj = db.Query(newTableMapping(typeof(MemberInfo)), string.Format("Select * from MemberInfo where ID={0}", id));

          if (obj != null&&obj.Count>0)

   {

     data = obj[0]  as MemberInfo;

  }

}

       return data;

}

    catch (Exception e)

{

      throw e;

}

}

  publicvoid Updata(MemberInfo data)

{

    try

{

      using (var db = newSQLiteConnection(path))

{

db.Update(data);

}

}

    catch(Exception e)

{

        throw e;

}

}

  publicObservableCollection<MemberInfo> SelectAll()

{

    ObservableCollection<MemberInfo> list = newObservableCollection<MemberInfo>();

    using (var db =newSQLiteConnection(path))

{

      List<object> query = db.Query(newTableMapping(typeof(MemberInfo)), "select * from MemberInfo");

       foreach (var mem in query)

{

          MemberInfo info = mem asMemberInfo;

    list.Add(info);

}

}

    return list;

}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章