65.9K
CodeProject is changing. Read more.
Home

Simple C# Wrapper for SQLite

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.25/5 (20 votes)

Aug 16, 2007

viewsIcon

156500

downloadIcon

6144

An article that describes lightweight C# wrapper for SQLite

Introduction

This article describes a very simple wrapper class for SQLite. This class provides only few simple functions: opening and closing database, returning the list of tables and executing queries. Although these are basic functions, they are enough for someone who needs only a storage engine for his/her program.

Background

While writing this class, I've been using ADO.NET Data Provider for SQLite as a reference, which I found here.

Using the Code

Using this class is very easy: add a reference to this DLL (Project->AddReference...) in your project and a line to your code:

using SQLWrapper;

Now you can start using database functions in your program.

Example:

// creates a new instance of SQLiteBase and opens database in file "test.db"

SQLiteBase db = new SQLiteBase("test.db");
// executes SELECT query and store results in new data table

DataTable table = db.ExecuteQuery("SELECT * FROM table1 WHERE id = 1;");
// closes the database

db.CloseDatabase();

Source Code

There are two constructors and and five public functions:

  • OpenDatabase
  • CloseDatabase
  • GetTables
  • ExecuteNonQuery
  • ExecuteQuery

which are all well-documented in the source file, so I don't see a point in explaining them again.