Click here to Skip to main content
15,870,130 members
Articles / Desktop Programming / Win32

Using SQLite in your C# Application

Rate me:
Please Sign up or sign in to vote.
4.51/5 (70 votes)
16 Dec 2007CPOL3 min read 986.2K   82.2K   239   52
SQLite is very suitable for memory constraint systems like WinCE, Palms, Smart Phones, embedded devices and also normal single user desktop applications.
This article briefly discusses what project possibilities there are for SQLite, which type of project are suited and which are not. It walks you through a practical example with a demo application that uses SQLite database to store id and description value pairs in a single table – mains. We will display the contents of this table in a grid with added functionalities of adding/deleting and modifying records to and from the table.

Introduction

SQLite is a small, fast and embeddable database where the database engine and the interface are combined into a single library. It also has the ability to store all the data in a single file. So if your application requires a standalone database, SQLite is perhaps the perfect choice for you. There are, of course, other reasons for choosing SQLite including:

  • SQLite has a small memory footprint and only a single library is required to access databases, making it ideal for embedded database applications.
  • SQLite has been ported to many platforms and runs even on Windows CE and Palm OS.
  • SQLite is ACID-compliant, meeting all four criteria - Atomicity, Consistency, Isolation, and Durability.
  • SQLite implements a large subset of the ANSI-92 SQL standard, including views, sub-queries and triggers.
  • No problem of extra database drivers, ODBC configuration required. Just include the library and the data file with your application.
  • SQLite has native language APIs for C/C++, PHP, Perl, Python, Tcl etc. Native API for C# is still not present.

Using the Code

To use SQLite in your C# application, you need to download a third party free ADO.NET data provider (SQLite.NET.0.21_x68_dll.zip) from here.

Screenshot - Executable

Fig 1: Demo C# SQLite application.

Our demo application uses SQLite database to store id and description value pairs in a single table – mains. We will display the contents of this table in a grid with added functionalities of adding/deleting and modifying records to and from the table. To set up the system, please follow the steps below:

  1. Unzip the archive, and place the *.dll files into the Binary folder.

  2. Use the SQLite.NET.dll in your .NET application by ‘Project -> Add Reference’.

  3. Add Using Finisar.SQLite; in the using directive declaration region of your *.cs file.

  4. Declare the following private fields to be used in your application:

    C#
    private SQLiteConnection sql_con;
    private SQLiteCommand sql_cmd;
    private SQLiteDataAdapter DB;
    private DataSet DS = new DataSet();
    private DataTable DT = new DataTable();
  5. Create a function to set up the Connection String. DemoT.db is the name of the single data file. It already has a single table – mains. Version (=3) is the version of the database engine.

    C#
    private void SetConnection() 
    { 
    sql_con = new SQLiteConnection
    	("Data Source=DemoT.db;Version=3;New=False;Compress=True;"); 
    } 
  6. Create a generic function ExecuteQuery, to execute Create Command queries.

    C#
    private void ExecuteQuery(string txtQuery) 
    { 
    SetConnection(); 
    sql_con.Open(); 
    sql_cmd = sql_con.CreateCommand(); 
    sql_cmd.CommandText=txtQuery; 
    sql_cmd.ExecuteNonQuery(); 
    sql_con.Close(); 
    }
  7. Now create a LoadData function to access the SQLite database and retrieve the data from the mains table and fill the Dataset. Please refer to the following code:

    C#
    private void LoadData() 
    { 
    SetConnection(); 
    sql_con.Open(); 
    sql_cmd = sql_con.CreateCommand(); 
    string CommandText = "select id, desc from mains"; 
    DB = new SQLiteDataAdapter(CommandText,sql_con); 
    DS.Reset(); 
    DB.Fill(DS); 
    DT= DS.Tables[0]; 
    Grid.DataSource = DT; 
    sql_con.Close(); 
    }
  8. To add/edit/delete an entry to and from the table, just pass the required query to the already created ExecuteQuery function. Please see the following code for an example:

    C#
    private void Add()
    {
    string txtSQLQuery = "insert into  mains (desc) values ('"+txtDesc.Text+"')";
    ExecuteQuery(txtSQLQuery);            
    }

To administer SQLite database, an open-source SQLite GUI Database Browser utility is of much use. It can be downloaded from here.

Conclusion

So should SQLite be used for all database driven applications? No.

Like all databases, SQLite has its list of shortcomings. It is not suitable for a client server application or as a networked database. It’s not suited well for a multi user scenario and can have serious file locking issues when accessed simultaneously over the network. Quite for the same reason, SQLite is not suited for a multi-threaded or a multi-process application-database access scenario.

To conclude, SQLite is very suitable for memory constraint systems like WinCE, Palms, Smart Phones, embedded devices and also normal single user desktop applications where its small memory footprint, single library and its copy and paste deployment feature give it a distinct advantage.

Pre-requisites

  1. Visual Studio .NET 2003 (C#)
  2. SQLite (www.sqlite.org)

History

  • 16-Dec-2007: Initial publication

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
Chayan Ray has been working as a Technical Consultant in a CMM level 5 company in India. His technical domain includes ASP.NET, C#, PHP, Perl, Cold Fusion, MySQL and MSSQL 2000.

Comments and Discussions

 
QuestionBLOB for sqlite db files Pin
Member 1459629330-Jun-20 22:51
Member 1459629330-Jun-20 22:51 
PraiseThanks Pin
Sameh Saeed Mohammad18-Apr-20 1:56
professionalSameh Saeed Mohammad18-Apr-20 1:56 
PraiseCODE Pin
Member 140756463-Dec-18 19:45
Member 140756463-Dec-18 19:45 
QuestionSe ha intentado cargar un programa con un formato incorrecto. (Excepción de HRESULT: 0x8007000B) Pin
José Carlos Tello Guevara20-Feb-18 9:18
José Carlos Tello Guevara20-Feb-18 9:18 
SuggestionRe: Se ha intentado cargar un programa con un formato incorrecto. (Excepción de HRESULT: 0x8007000B) Pin
Richard MacCutchan20-Feb-18 9:19
mveRichard MacCutchan20-Feb-18 9:19 
GeneralRe: Se ha intentado cargar un programa con un formato incorrecto. (Excepción de HRESULT: 0x8007000B) Pin
José Carlos Tello Guevara20-Feb-18 9:24
José Carlos Tello Guevara20-Feb-18 9:24 
GeneralRe: Se ha intentado cargar un programa con un formato incorrecto. (Excepción de HRESULT: 0x8007000B) Pin
Richard MacCutchan20-Feb-18 9:42
mveRichard MacCutchan20-Feb-18 9:42 
QuestionSqlite Android App Pin
Member 127461223-Jan-17 21:42
Member 127461223-Jan-17 21:42 
QuestionError when creating my DB Pin
Hosam Alaa7-Jul-16 2:35
Hosam Alaa7-Jul-16 2:35 
Question5 star Pin
James McCullough28-Mar-16 18:26
professionalJames McCullough28-Mar-16 18:26 
GeneralMy vote of 5 Pin
Member 352650824-Jan-16 1:25
Member 352650824-Jan-16 1:25 
QuestionError while Pin
Rawat JI4-Oct-15 19:04
Rawat JI4-Oct-15 19:04 
Questionadox add new column and name it from textbox or string variable dynamically Pin
Member 1106451130-Sep-14 19:38
Member 1106451130-Sep-14 19:38 
QuestionEasy step by step guide for how to use sqlite with Csharp With Source code Pin
heemanshubhalla4-Aug-14 3:22
heemanshubhalla4-Aug-14 3:22 
GeneralRe: Easy step by step guide for how to use sqlite with Csharp With Source code Pin
Patrick Harris4-Aug-14 5:16
Patrick Harris4-Aug-14 5:16 
GeneralRe: Easy step by step guide for how to use sqlite with Csharp With Source code Pin
heemanshubhalla4-Aug-14 7:43
heemanshubhalla4-Aug-14 7:43 
GeneralRe: Easy step by step guide for how to use sqlite with Csharp With Source code Pin
Patrick Harris4-Aug-14 11:29
Patrick Harris4-Aug-14 11:29 
AnswerSimplified All This Pin
Patrick Harris17-Jul-14 12:15
Patrick Harris17-Jul-14 12:15 
GeneralRe: Simplified All This Pin
EmmadKareem27-Sep-14 21:58
EmmadKareem27-Sep-14 21:58 
GeneralRe: Simplified All This Pin
EmmadKareem27-Sep-14 21:59
EmmadKareem27-Sep-14 21:59 
GeneralRe: Simplified All This Pin
Ghostdance13-Feb-17 12:55
Ghostdance13-Feb-17 12:55 
QuestionAdding the SQLite database Pin
Michael Murphy18-Mar-14 13:39
Michael Murphy18-Mar-14 13:39 
AnswerRe: Adding the SQLite database Pin
Fernando E. Braz4-May-14 0:15
Fernando E. Braz4-May-14 0:15 
QuestionUpdates for .NET 4.5.1? Pin
kiquenet.com28-Jan-14 4:22
professionalkiquenet.com28-Jan-14 4:22 
GeneralÇok İyiii Pin
İ.bilgic25-Dec-13 22:23
İ.bilgic25-Dec-13 22:23 
Sağolasın gardaş çok işime yaradı Thumbs Up | :thumbsup: Big Grin | :-D Roll eyes | :rolleyes:

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.