Articles → CSHARP → CRUD Operations In Sqlite Using C#
CRUD Operations In Sqlite Using C#
- Get data from sqlite table.
- Insert/update/delete in sqlite tables.
Import Sqlite Libraries
install-package system.data.sqlite.core -version 1.0.111
install-package system.data.sqlite.ef6 -version 1.0.111
install-package system.data.sqlite -version 1.0.111
Connectionstring For Sqlite
public string ConnectionString {
get {
return@"Data Source=<path>\<db_name>.db;Version=3;";
}
}
Method To Get Data From Table
public DataTable GetResultSetTable(string query) {
DataTable table = new DataTable();
using(SQLiteConnection connection = new SQLiteConnection(ConnectionString)) {
connection.Open();
using(SQLiteCommand command = new SQLiteCommand(query, connection)) {
using(SQLiteDataAdapter adapter = new SQLiteDataAdapter()) {
adapter.SelectCommand = command;
adapter.Fill(table);
}
}
}
return table;
}
Method To Perform Insert/Update/Delete Operation In The Table
public void ExecuteQuery(string query) {
using(SQLiteConnection connection = new SQLiteConnection(ConnectionString)) {
connection.Open();
using(SQLiteCommand command = new SQLiteCommand(query, connection)) {
command.ExecuteNonQuery();
}
}
}