Windows 8 Metro應用中使用C#連接SQLite及創建數據庫,數據表的增刪改查


  1.Metro中使用SQLite數據庫具體步驟如下:

  1).下載SQLite for WinRT

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

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

  2).為項目添加引用

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

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

  3). 為項目添加C# 驅動

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

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

  2.創建數據庫

  1).首先:聲明一個MemberInfo類也就是表主鍵自動增長

   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).寫一個方法用於創建數據庫Member.sqlite和表MemberInfo

    private void Create()

         {

      string path =Path.Combine(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;    

        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM