简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM