Windows 8 Metro用C#連接SQLite及創(chuàng)建數(shù)據(jù)庫,數(shù)據(jù)表的增刪改查的實現(xiàn)
1.Metro中使用SQLite數(shù)據(jù)庫具體步驟如下:
1).下載SQLite for WinRT
地址:http://www.sqlite.org/download.html
下載Precompiled Binaries for Windows Runtime,這是一個Visual Studio的一個擴(kuò)展,文件以vsix為后綴,直接雙擊運(yùn)行即可。(如下圖)
2).為項目添加引用
創(chuàng)建一個項目,在解決方案在選擇“引用->添加引用”,在引用管理器的左邊列表中選擇Windows->擴(kuò)展,然后再右邊的列表中選中如下圖所示:
注意:選擇 SQLite for Windows Runtime 和 Microsoft Visual C++ Runtime Package
3). 為項目添加C# 驅(qū)動
在解決方案中,選擇項目,單擊右鍵,選擇“管理NuGet程序包”,在管理器中進(jìn)行如下圖的操作:
安裝完成后,你的項目的根目錄下會多出兩個文件:SQLite.cs和SQLiteAsync.cs文件,我們就可以通過這兩個類來操作SQLite了。
2.創(chuàng)建數(shù)據(jù)庫
1).首先:聲明一個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; } } 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).寫一個方法用于創(chuàng)建數(shù)據(jù)庫Member.sqlite和表MemberInfo
{
3).簡單的操作sqlite數(shù)據(jù)庫(增,刪,改,查詢)
public void Insert(MemberInfo data)
相關(guān)文章
C# 導(dǎo)出Excel的6種簡單方法實現(xiàn)
C# 導(dǎo)出 Excel 的6種簡單方法:數(shù)據(jù)表導(dǎo)出到 Excel,對象集合導(dǎo)出到 Excel,數(shù)據(jù)庫導(dǎo)出到 Excel,微軟網(wǎng)格控件導(dǎo)出到 Excel,數(shù)組導(dǎo)出到 Excel,CSV 導(dǎo)出到 Excel,你都會了嗎?需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09在Framework 4.0中:找出新增的方法與新增的類(一)
經(jīng)常看到有同學(xué)在討論Framework 4 的新特性,新方法,于是想寫個程序找出framework4.0中新增的方法和類2013-05-05C#實現(xiàn)ListView選中項向上或向下移動的方法
這篇文章主要介紹了C#實現(xiàn)ListView選中項向上或向下移動的方法,通過兩個按鈕點擊事件實現(xiàn)ListView選中項的上下移動功能,需要的朋友可以參考下2015-06-06C#將數(shù)字轉(zhuǎn)換成字節(jié)數(shù)組的方法
這篇文章主要介紹了C#將數(shù)字轉(zhuǎn)換成字節(jié)數(shù)組的方法,涉及C#字符串操作的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04Unity3D使用陀螺儀控制節(jié)點旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Unity3D使用陀螺儀控制節(jié)點旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11