RT/Metro商店應用如何調用SQLite資料庫

來源:互聯網
上載者:User

標籤:

RT/Metro商店應用如何調用SQLite資料庫使用前,要安裝: SQLite for Windows Runtime (Windows 8.1)(一個VS外掛程式)、還有 Visual C++ Runtime Package(如:Microsoft Visual C++ 2013 Runtime Package for Windows),同時,項目產生要修改為X86或者X64,總之不能使有和AnyCPU。我這裡使用的是X86. 
  private async void Create()        {            //資料檔案儲存的位置            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db1.sqlite");            //開啟建立資料庫和表            using (var db = new SQLite.SQLiteConnection(dbPath))            {                //建立表                var result = db.CreateTable<Model.Person>();                await new MessageDialog("傳回值:" + result).ShowAsync();            }        }        private async void Insert()        {            //串連資料庫            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db1.sqlite");            using (var db = new SQLite.SQLiteConnection(dbPath))            {                //插入操作。首先聲明一個集合                ObservableCollection<Person> Collection = new ObservableCollection<Person>();                //單條插入語句                db.Insert(new Person() { FirstName = "宋興柱1", LastName = "Sindrol" });                Collection.Add(new Person() { FirstName = "宋興柱2", LastName = "Sindrol1" });                Collection.Add(new Person() { FirstName = "宋興柱3", LastName = "Sindrol2" });                //多條插入集合                var result = db.InsertAll(Collection);                await new MessageDialog("傳回值:" + result).ShowAsync();            }        }        private async void Update()        {            //更新語句            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db1.sqlite");            using (var db = new SQLite.SQLiteConnection(dbPath))            {                SQLiteCommand cmd = db.CreateCommand("update person set FirstName=‘lisa‘ where LastName=‘Sindrol‘");                var result = cmd.ExecuteNonQuery();                await new MessageDialog("傳回值:" + result).ShowAsync();            }        }        private async void Delete()        {            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db1.sqlite");            using (var db = new SQLite.SQLiteConnection(dbPath))            {                //單行刪除操作                db.Delete<Person>(1);                //多行刪除                var result = db.DeleteAll<Person>();                await new MessageDialog("傳回值:" + result).ShowAsync();            }        }        private async void Select()        {            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db1.sqlite");            using (var db = new SQLite.SQLiteConnection(dbPath))            {                //查詢所有資料繫結到UI                List<object> list = db.Query(new TableMapping(typeof(Person)), "select *  from  Person");                gridView.ItemsSource = list;            }        }

Person類如所示:

  class Person    {        [SQLite.AutoIncrement, SQLite.PrimaryKey]        public int ID { get; set; }        public string FirstName { get; set; }        public string LastName { get; set; }    }

資料庫:

 

RT/Metro商店應用如何調用SQLite資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.