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)增長 { [SQLite.AutoIncrement, SQLite.PrimaryKey] public int ID { set; get; } public string Name { set; get; } public int Age { set; get; } public string Address { set; get; } } 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>(); } } { 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; }
public class MemberInfo
2).寫一個(gè)方法用于創(chuàng)建數(shù)據(jù)庫Member.sqlite和表MemberInfo
{
3).簡單的操作sqlite數(shù)據(jù)庫(增,刪,改,查詢)
public void Insert(MemberInfo data)
相關(guān)文章
c#編寫webservice服務(wù)引用實(shí)例分享
c#編寫webservice服務(wù)引用實(shí)例分享,大家參考使用吧2013-12-12C# 導(dǎo)出Excel的6種簡單方法實(shí)現(xiàn)
C# 導(dǎo)出 Excel 的6種簡單方法:數(shù)據(jù)表導(dǎo)出到 Excel,對(duì)象集合導(dǎo)出到 Excel,數(shù)據(jù)庫導(dǎo)出到 Excel,微軟網(wǎng)格控件導(dǎo)出到 Excel,數(shù)組導(dǎo)出到 Excel,CSV 導(dǎo)出到 Excel,你都會(huì)了嗎?需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09在Framework 4.0中:找出新增的方法與新增的類(一)
經(jīng)??吹接型瑢W(xué)在討論Framework 4 的新特性,新方法,于是想寫個(gè)程序找出framework4.0中新增的方法和類2013-05-05C#實(shí)現(xiàn)ListView選中項(xiàng)向上或向下移動(dòng)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)ListView選中項(xiàng)向上或向下移動(dòng)的方法,通過兩個(gè)按鈕點(diǎn)擊事件實(shí)現(xiàn)ListView選中項(xiàng)的上下移動(dòng)功能,需要的朋友可以參考下2015-06-06C#將數(shù)字轉(zhuǎn)換成字節(jié)數(shù)組的方法
這篇文章主要介紹了C#將數(shù)字轉(zhuǎn)換成字節(jié)數(shù)組的方法,涉及C#字符串操作的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04基于WPF實(shí)現(xiàn)經(jīng)典紙牌游戲
這篇文章主要為大家詳細(xì)介紹了如何溧陽WPF實(shí)現(xiàn)經(jīng)典紙牌游戲,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)WPF有一定的幫助,需要的可以參考一下2023-02-02Unity3D使用陀螺儀控制節(jié)點(diǎn)旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Unity3D使用陀螺儀控制節(jié)點(diǎn)旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11