Articles → .NET → CRUD Operations In Sqlite Using C#

CRUD Operations In Sqlite Using C#





  1. Get data from sqlite table.
  2. 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();
    }
  }
}



Posted By  -  Karan Gupta
 
Posted On  -  Sunday, October 20, 2019

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250