即使是做網(wǎng)絡(luò)應(yīng)用,在斷線情況下,也需要考慮數(shù)據(jù)的本地存儲(chǔ)。在SQLite出現(xiàn)之前,數(shù)據(jù)量大的情況下,我們一直使用ACCESS,數(shù)據(jù)量小,則文件存儲(chǔ)。ACCESS不支持事務(wù)原子性,在斷電情況下(這種情況總是會(huì)發(fā)生)會(huì)導(dǎo)致數(shù)據(jù)很難恢復(fù)。
一:安裝
SQLITE,是一款輕型的數(shù)據(jù)庫(kù),是遵守ACID的關(guān)聯(lián)式數(shù)據(jù)庫(kù)管理系統(tǒng)。我直接使用的是 http://sqlite.phxsoftware.com/ (An open source ADO.NET provider for the SQLite database engine)。下載完畢是一個(gè)EXE,安裝后根目錄如下:
Bin下有一個(gè)測(cè)試工具,可以查看本地運(yùn)行SQLITE的各項(xiàng)性能指標(biāo)。
二:新建數(shù)據(jù)庫(kù)
安裝完畢后,打開(kāi)visual studio,新建數(shù)據(jù)連接,可以看到數(shù)據(jù)源多了一項(xiàng)SQLite。
新建連接,如下圖。SQLITE的數(shù)據(jù)庫(kù),保存后是一個(gè)文件。
三:數(shù)據(jù)庫(kù)維護(hù)
可以在VS中方面的維護(hù)SQLITE數(shù)據(jù),如下圖:
可以在VS中使用類似SQL查詢分析器的功能,如下圖:
四:混合模式
安裝完畢,可以直接在項(xiàng)目集的引用中,多了
System.Data.SQLite
System.Data.SQLite.Linq
兩個(gè)程序集,由于 http://sqlite.phxsoftware.com/ 的System.Data.SQLite是混合模式程序集,是針對(duì)“v2.0.50727”版的運(yùn)行時(shí)生成的,在沒(méi)有配置其他信息的情況下,無(wú)法在 4.0 運(yùn)行時(shí)中加載該程序集。故需要在App.config中配置如下參數(shù)。
<? xml version="1.0" encoding="utf-8" ?> < configuration > < startup useLegacyV2RuntimeActivationPolicy = "true" > < supportedRuntime version = "v4.0" /> </ startup > </ configuration >
五:SQLiteHelper
最后,提供一個(gè)自己寫的SQLiteHelper:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SQLite; using System.Data; using System.Data.Common; namespace Com.Luminji.DataService.SQLHelpers { public class SQLiteHelper { /// <summary> /// ConnectionString樣例:Data Source=Test.db3;Pooling=true;FailIfMissing=false /// </summary> public static string ConnectionString { get ; set ; } private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string cmdText, params object [] p) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Parameters.Clear(); cmd.Connection = conn; cmd.CommandText = cmdText; cmd.CommandType = CommandType.Text; cmd.CommandTimeout = 30; if (p != null ) { foreach ( object parm in p) cmd.Parameters.AddWithValue( string .Empty, parm); } } public static DataSet ExecuteQuery( string cmdText, params object [] p) { using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) { using (SQLiteCommand command = new SQLiteCommand()) { DataSet ds = new DataSet(); PrepareCommand(command, conn, cmdText, p); SQLiteDataAdapter da = new SQLiteDataAdapter(command); da.Fill(ds); return ds; } } } public static int ExecuteNonQuery( string cmdText, params object [] p) { using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) { using (SQLiteCommand command = new SQLiteCommand()) { PrepareCommand(command, conn, cmdText, p); return command.ExecuteNonQuery(); } } } public static SQLiteDataReader ExecuteReader( string cmdText, params object [] p) { using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) { using (SQLiteCommand command = new SQLiteCommand()) { PrepareCommand(command, conn, cmdText, p); return command.ExecuteReader(CommandBehavior.CloseConnection); } } } public static object ExecuteScalar( string cmdText, params object [] p) { using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) { using (SQLiteCommand command = new SQLiteCommand()) { PrepareCommand(command, conn, cmdText, p); return command.ExecuteScalar(); } } } } }
本文出處:
http://www.cnblogs.com/luminji/
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
