Search code examples
c#.netcompact-frameworksql-server-ce

SQL Server CE rollback does not undo delete


I am using SQL Server CE 3.5 and C# with the .NET Compact Framework 3.5. In my code I am inserting a row, then starting a transaction, then deleting that row from a table, and then doing a rollback on that transaction. But this does not undo the deletion. Why not? Here is my code:

SqlCeConnection conn = ConnectionSingleton.Instance;
conn.Open();
UsersTable table = new UsersTable();
table.DeleteAll();
MessageBox.Show("user count in beginning after delete: " + table.CountAll());
table.Insert(
new User(){Id = 0, IsManager = true, Pwd = "1234", Username = "Me"});
MessageBox.Show("user count after insert: " + table.CountAll());
SqlCeTransaction transaction = conn.BeginTransaction();
table.DeleteAll();
transaction.Rollback();
transaction.Dispose();
MessageBox.Show("user count after rollback delete all: " + table.CountAll());

The messages indicate that everything works as expected until the very end where the table has a count of 0 indicating the rollback did not undo the deletion.


Solution

  • I just got it answered on Microsoft's forum. You need to associate the SqlCeComand object with the transaction using the SqlCeCommand object's Transaction property.