SQL Server CE回滚不会撤消删除

sti*_*g_1 2 .net c# compact-framework sql-server-ce

我在.NET Compact Framework 3.5中使用SQL Server CE 3.5和C#.在我的代码中,我插入一行,然后启动一个事务,然后从表中删除该行,然后对该事务进行回滚.但这并没有撤消删除.为什么不?这是我的代码:

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());
Run Code Online (Sandbox Code Playgroud)

消息表明一切都按预期工作,直到表的计数为0表示回滚未撤消删除.

sti*_*g_1 6

我刚刚在微软的论坛上得到了解答.您需要使用SqlCeCommand对象的Transaction属性将SqlCeComand对象与事务相关联.

  • 正确 - 所有针对该连接运行的命令都不会自动登记连接上的事务.您必须告诉命令参与交易. (2认同)