I have a c# application with an SQLite database. It has a table called "Results_to_Risks" which I am trying to update. I first remove some rows in my local datatable, after which I use my update table function to update the table in the database.
foreach(var row in temp)
{
allResToRisks.Rows.Remove(row);
}
_databaseController.UpdateTable(allResToRisks, "Results_to_Risks");
My database controller update table method:
// Updates an entire database table.
public void UpdateTable(DataTable datatable, string table, string condition = "")
{
using (SQLiteConnection dbConnection = new SQLiteConnection(conString))
{
using (var sqliteAdapter = new SQLiteDataAdapter("SELECT * FROM '" + @table + "'" + @condition, dbConnection))
{
dbConnection.Open();
using(var builder = new SQLiteCommandBuilder(sqliteAdapter))
{
sqliteAdapter.Update(datatable);
}
}
}
}
In this particular case, I remove just one row. The datatable ends up empty (Rows.Count == 0), because this was the only row in that datatable. So nothing goes wrong there. However my table in the database still has the one row inside it.
What am I doing wrong?
I saw this on the MDSN website:
It is important to understand the difference between deleting a row in a DataTable and removing the row. When you call the Remove or RemoveAt method, the row is removed immediately. Any corresponding rows in the back end data source will not be affected if you then pass the DataTable or DataSet to a DataAdapter and call Update. When you use the Delete method, the row remains in the DataTable and is marked for deletion. If you then pass the DataTable or DataSet to a DataAdapter and call Update, the corresponding row in the back end data source is deleted.
So i tried to use Delete()
instead of Remove()
. However, this made no difference.