欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Windows 8 Metro用C#連接SQLite及創(chuàng)建數(shù)據(jù)庫,數(shù)據(jù)表的增刪改查的實(shí)現(xiàn)

 更新時(shí)間:2013年04月25日 09:27:09   作者:  
本篇文章小編為大家介紹,Windows 8 Metro用C#連接SQLite及創(chuàng)建數(shù)據(jù)庫,數(shù)據(jù)表的增刪改查的實(shí)現(xiàn)。需要的朋友參考下

  1.Metro中使用SQLite數(shù)據(jù)庫具體步驟如下:

  1).下載SQLite for WinRT

  地址:http://www.sqlite.org/download.html

  下載Precompiled Binaries for Windows Runtime,這是一個(gè)Visual Studio的一個(gè)擴(kuò)展,文件以vsix為后綴,直接雙擊運(yùn)行即可。(如下圖)

  2).為項(xiàng)目添加引用

  創(chuàng)建一個(gè)項(xiàng)目,在解決方案在選擇“引用->添加引用”,在引用管理器的左邊列表中選擇Windows->擴(kuò)展,然后再右邊的列表中選中如下圖所示:

注意:選擇 SQLite for Windows Runtime 和 Microsoft Visual C++ Runtime Package

  3). 為項(xiàng)目添加C# 驅(qū)動(dòng)

   在解決方案中,選擇項(xiàng)目,單擊右鍵,選擇“管理NuGet程序包”,在管理器中進(jìn)行如下圖的操作:

安裝完成后,你的項(xiàng)目的根目錄下會(huì)多出兩個(gè)文件:SQLite.cs和SQLiteAsync.cs文件,我們就可以通過這兩個(gè)類來操作SQLite了。

  2.創(chuàng)建數(shù)據(jù)庫

  1).首先:聲明一個(gè)MemberInfo類也就是表主鍵自動(dòng)增長

復(fù)制代碼 代碼如下:

   public class MemberInfo

     {

           [SQLite.AutoIncrement, SQLite.PrimaryKey]

       public int ID { set; get; }

       public string Name { set; get; }

             public int Age { set; get; }

       public string Address { set; get; }

     }


  2).寫一個(gè)方法用于創(chuàng)建數(shù)據(jù)庫Member.sqlite和表MemberInfo
復(fù)制代碼 代碼如下:

         {

      string path =Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Member.sqlite");    //數(shù)據(jù)文件保存的位置 

      using (var db = new SQLite.SQLiteConnection(path))  //打開創(chuàng)建數(shù)據(jù)庫和表

             {

        db.CreateTable<MemberInfo>();

              }

          }



  3).簡單的操作sqlite數(shù)據(jù)庫(增,刪,改,查詢)
復(fù)制代碼 代碼如下:

     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;    

        }

相關(guān)文章

最新評(píng)論