private void readAllRecords()
{
try
{
var datatable = new DataTable();
myConnection.Open();
string commandText = "Select * From Albums";
SQLiteDataAdapter mySqliteDataAdapter = new SQLiteDataAdapter(commandText, myConnection);
mySqliteDataAdapter.Fill(datatable);
myConnection.Close();
dataGridView.DataSource = datatable;
}
{
throw;
}
}
{
try
{
myConnection.Open();
SQLiteCommand myCommand = new SQLiteCommand(myConnection);
myCommand.CommandText = "Delete FROM Albums WHERE AlbumName = '" + this.txtAlbumTitle.Text + "' ";
//Must click in AlbumTitle cell per above line to get delete to work
myCommand.ExecuteNonQuery();
myConnection.Close();
//MessageBox.Show("Your record has been deleted.");
txtArtistName.Text = " Artist Name";
txtAlbumTitle.Text = " Album Title";
txtYear.Text = " Year";
cmbxGenre.Text = " Genre";
cmbxMedium.Text = " Medium";
btnSave.Enabled = false;
readAllRecords();
}
catch (Exception)
{
}
}
{
if (!row.IsNewRow)
dataGridView.Rows.Remove(row);
}

VulpesPosted Dec 5, 2014, 5:14 PM
If you construct one of these and pass it a DataAdapter and, if you also set the latter's SelectCommand property, then the CommandBuilder will automatically generate the appropriate INSERT, UPDATE and DELETE commands when the DataAdapter's Update statement is invoked.
To do this the database table needs to have a primary key or a unique column and the CommandBuilder gets the rest of the information it needs from the SelectCommand property.
The DataAdapter knows which rows need updating because flags are set internally each time you insert, update or delete a row from the table or a control to which its bound.
Although I know this stuff works with the DataAdapters for SQL Server, ODBC and Oracle I wasn't 100% sure it would work with SQLite as well but it seems that it does :)
VulpesPosted Dec 5, 2014, 5:29 PM
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder%28v=vs.110%29.aspx
Although this is for SQL Server, it now seems safe to assume that it works the same for SQLite or any other database for which there is an ADO.NET provider.
Stan SypekPosted Dec 5, 2014, 5:23 PM
Stan SypekPosted Dec 5, 2014, 5:02 PM
VulpesPosted Dec 5, 2014, 4:40 PM
Stan SypekPosted Dec 5, 2014, 3:51 PM
**Edit** I was not calling the update method before. I was just using the code as is, and it worked, just not the way I wanted as mentioned in the post.
VulpesPosted Dec 5, 2014, 10:54 AM
However, as this is currently a local variable, you'll need to move it to form level first.
So:
private SQLiteDataAdapter mySqliteDataAdapter; // now a form level variable
private void readAllRecords()
{
try
{
var datatable = new DataTable();
myConnection.Open();
string commandText = "Select * From Albums";
mySqliteDataAdapter = new SQLiteDataAdapter(commandText, myConnection); // now using form level variable
mySqliteDataAdapter.Fill(datatable);
myConnection.Close();
dataGridView.DataSource = datatable;
}
catch (Exception)
{
throw;
}
}
The code to delete the selected rows will then be:
foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
if (!row.IsNewRow)
dataGridView.Rows.Remove(row);
}
DataTable datatable = (DataTable)dataGridView.DataSource;
mySqliteDataAdapter.Update(datatable);