As you know SQLite is light weight database no need of any set up configuration and easy to use. If you already used SQLite with windows/Phone apps then you are almost there. For this post I assume that you had already worked with SQLite if not at least with SQL server for your application. There is no any complex steps to follow for doing this in WPF. It’s almost same like what you did before. Here is step by step guide for using SQLite in WPF application.
- Install SQLite package from Nuget in your project. This package works with both 32/64 bit version of your machine.
- Once it’s installed start with making connection string and write some query with SQLite.
- Connection string looks like this. Add System.Data.SQLite namespace.
C#1SQLiteConnection conn = new SQLiteConnection("Data Source=Student.db");
Remember that you have to provide database name as data source like shown in connection string i.e. “Data Source=Student.db”. You can see your created database in Debug folder of your application and with any SQLite browser application you can view it, can do query on it, make changes on it etc. etc.
Now here is basic CRUD operations in SQLite database.
var command = conn.CreateCommand();
command.CommandText = "CREATE TABLE IF NOT EXISTS std_tbl(id int, Name Varchar(30))";
command.CommandText = "INSERT INTO std_tbl(id, Name ) values (1, 'janak')";
command.CommandText = "UPDATE std_tbl SET Name='Zasmik' WHERE id = 1";
//Read from table
command.CommandText = "SELECT * FROM std_tbl";
SQLiteDataReader sdr = command.ExecuteReader();
command.CommandText = "DELETE FROM std_tbl WHERE id=1";
As you can see there is no new concept for using SQLite in WPF. You can see the connection string like of SQL with even less configuration(only passing database name as data source), Class and Methods like SQLiteDataReader, SQLiteCommand, ExecuteNonQuery, ExecuteDataReader all like what we used while using SQL server database. In case of query its exactly like SQL query. So this is how you can use SQLite in WPF application.
[Tips] If you need to encrypt data before storing in database then visit my previous post to learn how to encrypt and decrypt data in c#.
Thats it. Happy Coding 🙂