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);
}


VulpesRank #55 · 29.2K reputationPosted Dec 5, 2014, 12:14 PM EST
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 :)
VulpesRank #55 · 29.2K reputationPosted Dec 5, 2014, 11:40 AM EST
VulpesRank #55 · 29.2K reputationPosted Dec 5, 2014, 5:54 AM EST
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);
Stan Sypek13 reputationPosted Dec 5, 2014, 10:51 AM EST
**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.
Stan Sypek13 reputationPosted Dec 5, 2014, 12:02 PM EST
Stan Sypek13 reputationPosted Dec 5, 2014, 12:23 PM EST
VulpesRank #55 · 29.2K reputationPosted Dec 5, 2014, 12:29 PM EST
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.