C# 資料本機存放區方案之SQLite

來源:互聯網
上載者:User

 

即使是做網路應用,在斷線情況下,也需要考慮資料的本機存放區。在SQLite出現之前,資料量大的情況下,我們一直使用ACCESS,資料量小,則檔案儲存體。ACCESS不支援事務原子性,在斷電情況下(這種情況總是會發生)會導致資料很難恢複。

 

一:安裝

SQLITE,是一款輕型的資料庫,是遵守ACID的關聯式資料庫管理系統。我直接使用的是http://sqlite.phxsoftware.com/(An open source ADO.NET provider for the SQLite database engine)。下載完畢是一個EXE,安裝後根目錄如下:

Bin下有一個測試載入器,可以查看本地運行SQLITE的各項效能指標。

 

二:建立資料庫

安裝完畢後,開啟visual studio,建立資料連線,可以看到資料來源多了一項SQLite。

建立串連,如。SQLITE的資料庫,儲存後是一個檔案。

 

三:資料庫維護

可以在VS中方面的維護SQLITE資料,如:

可以在VS中使用類似SQL查詢分析器的功能,如:

 

 

四:混合模式

安裝完畢,可以直接在項目集的引用中,多了

System.Data.SQLite

System.Data.SQLite.Linq

兩個程式集,由於http://sqlite.phxsoftware.com/的System.Data.SQLite是混合模式程式集,是針對“v2.0.50727”版的運行時產生的,在沒有配置其他資訊的情況下,無法在 4.0 運行時中載入該程式集。故需要在App.config中配置如下參數。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>

 

五:SQLiteHelper

最後,提供一個自己寫的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範例:Datasource=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();
}
}
}

}
}

 

六:附註SQLite官方網站: http://www.sqlite. org/ 時第一眼看到關於SQLite的特性。  1. ACID事務  2. 零配置 – 無需安裝和管理配置  3. 儲存在單一磁碟檔案中的一個完整的資料庫  4. 資料庫檔案可以在不同位元組順序的機器間自由的共用  5. 支援資料庫大小至2TB   6. 足夠小, 大致3萬行C代碼, 250K   7. 比一些流行的資料庫在大部分普通資料庫操作要快   8. 簡單, 輕鬆的API   9. 包含TCL綁定, 同時通過Wrapper支援其他語言的綁定   10. 良好注釋的原始碼, 並且有著90%以上的測試覆蓋率   11. 獨立: 沒有額外依賴   12. Source完全的Open, 你可以用於任何用途, 包括出售它   13. 支援多種開發語言,C, PHP, Perl, Java, ASP .NET,Python

來源:http://www.cnblogs.com/luminji/archive/2010/12/19/1910396.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.