Articles → .NET → CRUD operations in sqlite using C#

CRUD operations in sqlite using C#










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