views:

346

answers:

2

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.

+2  A: 

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.

INTPnerd
Correct - transactions on the connection are not automatically enlisted by all commands being run against that connection. You have to tell the command to participate in the transaction.
Jason Short
A: 

I know it is a while ago this is answered but I have a question regarding the use of the SqlCeCommand object. I'm using the tableadapter or a sqldatabase table. And when I use a query created for the table I don't have a SqlCeCommand. So how can I connect the transaction to the datatable?

Kind regards

JKorsten

JKorsten
You should ask your own question for this instead of posting it as an answer to the original question.
INTPnerd