標籤: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資料庫操作詳解