windows phone 8.1開發SQlite資料庫操作詳解

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   for   資料   div   

原文出自:http://www.bcmeng.com/windows-phone-sqlite1/

本文小夢將和大家分享WP8.1中SQlite資料庫的基本操作:(最後有整個樣本的源碼)(希望能通過本站廣告支援小夢,謝謝!)

  • 建立資料庫
  • 增加資料
  • 刪除資料
  • 更改資料
  • 查詢資料

(注:為了讓每個操作都能及時顯示在UI上,所以進行了資料繫結.資料繫結會在後面文章專門講解,先給出資料類Note,代表一個筆記.含有Name 和content  屬性.其代碼如下:如果不清楚,我會在之後講解):

namespace SQlite{    public class Note : INotifyPropertyChanged    {           private int id;           [AutoIncrement, PrimaryKey]            public int ID            {               get { return id; }                set            {                if (value!=id)                {                    id = value;                    RaisePropertyChanged("ID");                }            }           }           private string name;            [MaxLength(30)]           public string Name           {               get { return name; }               set               {                   if (value != name)                   {                       name = value;                       RaisePropertyChanged("Name");                   }               }           }            private string content;            [MaxLength(300)]            public string Content            {                get { return content; }                set                {                    if (value != content)                    {                        content = value;                        RaisePropertyChanged("Content");                    }                }            }            public event PropertyChangedEventHandler PropertyChanged;            protected void RaisePropertyChanged(string propertyName)            {                if (PropertyChanged != null)                {                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));                }            }    }}
windows phone 8.1開發SQlite-建立資料庫
private SQLiteAsyncConnection GetConn()        {            return new SQLiteAsyncConnection(ApplicationData.Current.LocalFolder.Path + "\\note.db");        }        private  async void createButton_Click(object sender, RoutedEventArgs e)        {            SQLiteAsyncConnection conn = GetConn();             await conn.CreateTableAsync<Note>();            await new MessageDialog("建立資料庫成功!").ShowAsync();        }
windows phone 8.1開發SQlite-增加資料
SQLiteAsyncConnection conn = GetConn();            await conn.InsertAsync(new Note { Name = "小夢", Content = "小夢我想你" });            await conn.InsertAsync(new Note { Name = "小夢", Content = "小夢我愛你" });            await conn.InsertAsync(new Note { Name = "小夢", Content = "小夢我喜歡你" });            await conn.InsertAsync(new Note { Name = "小夢", Content = "小夢我恨你" });            await conn.InsertAsync(new Note { Name = "小夢", Content = "小夢我打你" });            List<Note> notelist = await conn.Table<Note>().ToListAsync();            notes = new ObservableCollection<Note>(notelist);            listBox.ItemsSource = notes;            await new MessageDialog("增加資料成功!").ShowAsync();
windows phone 8.1開發SQlite-刪除資料
SQLiteAsyncConnection conn = GetConn();            var query =  await conn.Table<Note>().FirstAsync();            for (int i = 0; i < notes.Count;i++ )            {                if (notes[i].ID == query.ID)                {                    notes.RemoveAt(i);                    break;                }            }            await conn.DeleteAsync(query as Object);            listBox.ItemsSource = notes;            await new MessageDialog("刪除資料成功!").ShowAsync();
windows phone 8.1開發SQlite-修改資料
SQLiteAsyncConnection conn = GetConn();            for (int i = 0; i < notes.Count; i++)            {                if (notes[i].Name=="小夢")                {                    notes[i].Content = "小夢我愛你";                }            }            var query = conn.Table<Note>().Where(x => x.Name=="小夢");            var result = await query.ToListAsync();            foreach (var item in result)            {                item.Content = "小夢我愛你";                await conn.UpdateAsync(item);            }            await new MessageDialog("更新資料成功!").ShowAsync();
windows phone 8.1開發SQlite-查詢資料
SQLiteAsyncConnection conn = GetConn();            var query = conn.Table<Note>();            var result = await query.ToListAsync();            notes = new ObservableCollection<Note>(result);            listBox.ItemsSource = notes;            await new MessageDialog("查詢資料成功!").ShowAsync();
windows phone 8.1開發SQlite資料庫操作詳解源碼下載:

點我下載!

windows phone 8.1開發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.