ASP.NET: Using SQLite with Enterprise Library 3.1
By Peter Bromberg
Summary of how to use the EntlibContrib SQLite provider with Enterprise Library 3.1 (May 2007) and ASP.NET
Information and download of System.Data.SqLite:
http://sqlite.phxsoftware.com/
Enterprise Library 3.1 (May 2007):
http://www.microsoft.com/downloads/details.aspx?FamilyID=4c557c63-708f-4280-8f0c-637481c31718&displaylang=en
EntLibContrib Project at codeplex:
http://www.codeplex.com/entlibcontrib
I've extolled the virtues of the amazing SQLite database engine a number of times here (just use our Search widget with "SQLITE") so I won't go into much detail about it. Here are a few bullet points, though:
- Complete ADO.NET 2.0 Implementation
- Supports the Full and Compact .NET Framework as well as native C/C++
- Completely portable database files
- Incredibly fast, faster than most every other embedded database, including Sql Server Mobile
- Encryption Support
- Full Text Search
- Visual Studio 2005 Design-Time Support
- Single file redistributable under 500kb -- nothing to "install"!
- Extensive SQL support
- User-Defined Functions & Collating Sequences, Triggers
- Full Source Included. 100% Free
You can thank my friend Robert Simpson for all this effort and contribution. In sum, SQLite is very fast, requires no configuration or installation (only that the assembly be present), and implements most all of the SQL 92 spec. With the ADO.NET Provider, you are dealing with datareaders, DataSets and all the familiar programming tools you are used to. The SQL syntax is a bit of a variant from T-SQL, but the differences are quite minor.
At Codeplex, there is a Enterprise Library "contrib" project with a number of enhancements written by users like you and me. One in particular that caught my attention was the SQLite provider, written by Ken Scott http://freqken.net/.
The solution here shows how to wire up your Entlib configuration to use the ADO.NET 2.0 System.Data.SqLite engine with Enterprise Library 3.1
First you will need the required EntLib and Contrib assemblies:
Microsoft.Practices.EnterpriseLibrary.Data
Microsoft.Practices.EnterpriseLibrary.Common
EnterpriseLibrary.Contrib.Data.SqLite
EnterpriseLibrary.Contrib.Data
Then, you'll set up your configuration, which can be done with the EntLib Configuration Add-on from within Visual Studio:

My sample config looks like this:
<?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 defaultDatabase="ConnectionString">
<providerMappings>
<add databaseType="Microsoft.Practices.EnterpriseLibrary.Contrib.Data.SqLite, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="System.Data.SqLite" />
</providerMappings>
</dataConfiguration>
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=dbpath;New=True;UTF8Encoding=True;Version=3"
providerName="System.Data.SQLite" />
</connectionStrings>
<appSettings>
<add key="Setting" value="" />
</appSettings>
<system.web>
<compilation debug="true" />
<authentication mode="Windows" />
</system.web>
</configuration>
Finally, I've got two sample classes that I ripped right out of the test fixtures from the Contrib project, and massaged to fit this demo:
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
namespace SQLiteEntLib
{
public class TestConfigurationSource
{
public static DictionaryConfigurationSource CreateConfigurationSource()
{
DictionaryConfigurationSource source = new DictionaryConfigurationSource();
DatabaseSettings settings = new DatabaseSettings();
settings.DefaultDatabase = "Quotes";
ConnectionStringsSection section = new ConnectionStringsSection();
string dbPath = System.Web.HttpContext.Current.Server.MapPath("quotes.db3");
section.ConnectionStrings.Add(new ConnectionStringSettings("ConnectionString", @"Data Source=" + dbPath , "System.Data.SQLite"));
source.Add(DatabaseSettings.SectionName, settings);
source.Add("connectionStrings", section);
return source;
}
}
}
using System;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
namespace SQLiteEntLib
{
public class SQLiteExecuteDatasetHelper
{
private const string queryString = "Select * from quotes where authorname like 'a%'";
private Database db;
public DataSet ExecuteDataSetFromSqlString()
{
DatabaseProviderFactory factory = new DatabaseProviderFactory(TestConfigurationSource.CreateConfigurationSource());
db = factory.Create("ConnectionString");
DataSet dataSet = db.ExecuteDataSet(CommandType.Text, queryString);
return dataSet;
}
}
}
And that's it! The Database object has most of the same familiar methods that are present for all the other Providers. The only thing you don't have is stored procedures.
However, based on all the "stored procs are evil" diatribe I've listened to and read, that could be a good thing!
Note that I am doing a string Replace on the "dbPath" string with Server.MapPath and the name of the database file to make this more portable for ASP.NET.
The download has a small SQLite database of quotations by famous authors (a subset, for size) and the ASP.NET app displays the results of a query in a GridView. You can run this without downloading anything as it already contains all the required assemblies. Just unzip, load into Visual Studio 2005, and press the green button!
Of course, I recommend that you download the three projects and sources from the links at the very top of this article and install each so you can look at code and learn more.
Popularity (2881 Views)
 |
| Biography - Peter Bromberg |
Peter Bromberg is a C# MVP, MCP, and .NET expert who has worked in banking, financial and telephony for over 20 years. Pete focuses exclusively on the .NET Platform, and currently develops SOA and other .NET applications for a Fortune 500 clientele. Peter enjoys producing digital photo collage with Maya,playing jazz flute, the beach, and fine wines. You can view Peter's UnBlog and IttyUrl sites.
|  |
|
|
Article Discussion: ASP.NET: Using SQLite with Enterprise Library 3.1
Stored Procedures Are Evil
Brian Rush replied
to Peter Bromberg at Wednesday, July 25, 2007 7:48 AM
Huh? Stored Procedures are now evil? Sort of like milk is bad ..no wait good..no wait bad for you. Any merit to Stored Procedures are bad?
Peter's commentary on stored procedures was from his blog
Robbe Morris replied
to Peter Bromberg at Wednesday, July 25, 2007 7:48 AM
Folks have voiced some interesting opinions on the subject.
This is the background on that
Peter Bromberg replied
to Brian Rush at Wednesday, July 25, 2007 7:48 AM
CAB in Enterprise Library
rish vnjash replied
to Peter Bromberg at Wednesday, July 25, 2007 7:48 AM
Microsoft come up with Enterprise library 4.0 but constraints of scalibility and performance layed behind .In addition to the mentioned article these drawback can be get on the way by using caching applications specialy NCache .NCache Express is free and promise good deal of performance without anycost.
CAB in Enterprise Library
rish vnjash replied
to Peter Bromberg at Wednesday, July 25, 2007 7:48 AM
Microsoft come up with Enterprise library 4.0 but constraints of scalibility and performance layed behind .In addition to the mentioned article these drawback can be get on the way by using caching applications specialy NCache .NCache Express is free and promise good deal of performance without anycost.www.alachisoft.com/ncache/cab_index.html