Saturday, 24 August 2013

Need help on deleting row(s) from SQLCE Database

Need help on deleting row(s) from SQLCE Database

another question today. This time, I'm having trouble deleting a row from
an SQLCE database.
private void Form1_Load(object sender, EventArgs e)
{
// Create a connection to the file datafile.sdf in the program folder
string dbfile = new
System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName
+ "\\userDtbs.sdf";
SqlCeConnection connection = new SqlCeConnection("datasource=" +
dbfile);
// Read all rows from the table test_table into a dataset (note,
the adapter automatically opens the connection)
SqlCeDataAdapter adapter = new SqlCeDataAdapter("SELECT * FROM
history", connection);
DataSet data = new DataSet();
adapter.Fill(data);
//Delete from the database
using (SqlCeCommand com = new SqlCeCommand("DELETE FROM accounts
WHERE Id = 0", connection))
{
com.ExecuteNonQuery();
}
// Save data back to the databasefile
var cmd = new SqlCeCommandBuilder(adapter);
adapter.Update(data);
// Close
connection.Close();
}
My program's giving me an error telling me that connection is in a closed
state, and I can't figure out why it would close before the DELETE command
is executed.

No comments:

Post a Comment