[Entlib][Contrib] 使用 SQLite

[Entlib][Contrib] 使用 SQLite

在 .NET Framework 2.0 環境使用 SQLite,需準備下列套件

(1) Enterprise Library 3.1 – May 2007 (Entlib)

(2) Enterprise Library Contrib v3.1 May 2009 (Entlib Contrib)

Binary 版本僅支援 System.Data.SQLite v1.0.61.0;若要使用System.Data.SQLite 新的版本,須下載 Source 版本並重新編譯(組件參考 System.Data.SQLite.dll 最新版本,已編譯可支援 System.Data.SQLite v1.0.66.0 的 EntLibContrib.Data.SQLite.dll)。

(3) System.Data.SQLite

將原廠 SQLite 資料庫引擎與 ADO.NET 2.0/3.5 provider 整合至單一組件(System.Data.SQLite.dll),下載 Binary 版本使用而不用再引用 sqlite3.dll。

(4) SQLite Database Browser

是一套免費的 SQLite GUI 工具,讓你輕鬆的建立 SQLite 資料庫。

需要用到的組件

Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Data.dll
Microsoft.Practices.ObjectBuilder.dll
EntLibContrib.Common.dll
EntLibContrib.Data.SQLite.dll
System.Data.SQLite.DLL

連線字串設定

開啟『Enterprise Library Configuration』讀取組態檔(app.config/web.config),設定 Connection Strings 與 Custom Provider Mappings。

[Connection Strings]

Name:SqliteConnectionString
ConnectionString:Data Source=|DataDirectory|\yourDB.db;Version=3;
ProviderName:System.Data.SQLite

Data Source=|DataDirectory|\yourDB.db 是用於 Website 的 App_Data 路徑,應用程式則是要用絕對路徑才能找到。

 

[Custom Provider Mappings]

Name:System.Data.SQLite
TypeName:EntLibContrib.Data.SQLite.SQLiteDatabase, EntLibContrib.Data.SQLite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

填寫 TypeName 時利用 Type Selector 視窗載入『System.Data.SQLite.DLL』以便自動取得類型名稱。

完成後,組態檔的內容也自動產生,如下列表,存取資料庫的方式與 Data Access Application Block 一樣,因此不再多作說明。


<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
  </configSections>
  <dataConfiguration>
    <providerMappings>
      <add databaseType="EntLibContrib.Data.SQLite.SQLiteDatabase, EntLibContrib.Data.SQLite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="System.Data.SQLite"/>
    </providerMappings>
  </dataConfiguration>
  <connectionStrings>
    <add name="SqliteConnectionString" connectionString="Data Source=|DataDirectory|\yourDB.db;Version=3;" providerName="System.Data.SQLite"/>
  </connectionStrings>
</configuration>

Sample
Entlib.SQLite.Sample.zip