167

Within C# application code, I would like to create and then interact with one or more SQLite databases.

How do I initialize a new SQLite database file and open it for reading and writing?

Following the database's creation, how do I execute a DDL statement to create a table?

Andrew
  • 16,273
  • 10
  • 93
  • 104
TinKerBell
  • 2,023
  • 2
  • 13
  • 12

1 Answers1

317

The next link will bring you to a great tutorial, that helped me a lot!

How to SQLITE in C#

I nearly used everything in that article to create the SQLite database for my own C# Application.

Don't forget to download the SQLite.dll, and add it as a reference to your project. This can be done using NuGet and by adding the dll manually.

After you added the reference, refer to the dll from your code using the following line on top of your class:

using System.Data.SQLite;

You can find the dll's here:

SQLite DLL's

You can find the NuGet way here:

NuGet

Up next is the create script. Creating a database file:

SQLiteConnection.CreateFile("MyDatabase.sqlite");

SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();

string sql = "create table highscores (name varchar(20), score int)";

SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();

sql = "insert into highscores (name, score) values ('Me', 9001)";

command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();

m_dbConnection.Close();

After you created a create script in C#, I think you might want to add rollback transactions, it is safer and it will keep your database from failing, because the data will be committed at the end in one big piece as an atomic operation to the database and not in little pieces, where it could fail at 5th of 10 queries for example.

Example on how to use transactions:

 using (TransactionScope tran = new TransactionScope())
 {
     //Insert create script here.

     //Indicates that creating the SQLiteDatabase went succesfully, so the database can be committed.
     tran.Complete();
 }
Max
  • 11,885
  • 15
  • 70
  • 96
  • 5
    Nice clear answer. +1'd. Here's another example that shows you just how quick SQLite can be in inserting and retrieving records: http://www.technical-recipes.com/2016/using-sqlite-in-c-net-environments/ – AndyUK Apr 15 '17 at 09:10
  • In my test using `System.Transactions.TransactionScope` doesn't work as expected, it will execute every `ExecuteNonQuery` immediately and not all together, as `SQLiteTransaction`. Why use `TransactionScope`? – MrCalvin Mar 02 '19 at 11:08
  • 1
    I prefer `SQLiteTransaction tr = m_dbConnection.BeginTransaction(); SQLiteCommand command = new SQLiteCommand(...); command.Transaction = tr;` over using `TransactionScope` – user643011 Aug 01 '19 at 23:47
  • Sql transactions are for data statements only. DDL is never a part of a transaction – Boppity Bop Apr 30 '21 at 22:57
  • Is it possible Max's answer was wrong about using TransactionScope()? – James Kerfoot Apr 17 '22 at 19:16