将 TransactionScope 与类型化数据集结合使用
我有一个 WinForm 应用程序,使用类型化数据集表适配器查询 SqlCe 数据库。 我有一个主窗体程序集和一个处理每个数据库操作的数据库程序集。 我在事务中使用 tableadapter 进行更新时遇到问题,如果有人能给我任何想法,我将不胜感激。
Update() 方法给出此错误:
"The connection object can not be enlisted in transaction scope."
这是我的代码:
namespace Main
{
public class MainForm
{
private MyDbAssembly.MyDbClass db;
//instantiate and db fill methods omitted..
private void DeleteStuff()
{
using (TransactionScope trans = new TransactionScope())
{
this.db.Delete(id);
UpdateDb();
trans.Complete();
}
}
private void UpdateDb()
{
//bindingsource endedit & datagridview endedit methods omitted..
this.db.Update();
}
}
}
namespace MyDbAssembly
{
public class MyDbClass
{
private myTypedDataset myDataSet;
private myTypedDataSetTableAdapter.MyTable1Adapter table1Adapter;
//instantiate methods omitted..
public void Delete(Guid id)
{
this.myDataSet.MyTable1.FindByID(id).Delete();
}
public void Update()
{
this.table1Adapter.Update(myDataSet.MyTable); //<-- ERROR LINE
}
}
}
I have a WinForm application querying SqlCe database using typed-dataset tabledapters. I have a main form assembly and a database assembly which handles every db operation. I'm having problems with updating using tableadapter in a transaction and I'd appreciate if anyone could give me any ideas why.
Update() method gives this error:
"The connection object can not be enlisted in transaction scope."
Here's my code:
namespace Main
{
public class MainForm
{
private MyDbAssembly.MyDbClass db;
//instantiate and db fill methods omitted..
private void DeleteStuff()
{
using (TransactionScope trans = new TransactionScope())
{
this.db.Delete(id);
UpdateDb();
trans.Complete();
}
}
private void UpdateDb()
{
//bindingsource endedit & datagridview endedit methods omitted..
this.db.Update();
}
}
}
namespace MyDbAssembly
{
public class MyDbClass
{
private myTypedDataset myDataSet;
private myTypedDataSetTableAdapter.MyTable1Adapter table1Adapter;
//instantiate methods omitted..
public void Delete(Guid id)
{
this.myDataSet.MyTable1.FindByID(id).Delete();
}
public void Update()
{
this.table1Adapter.Update(myDataSet.MyTable); //<-- ERROR LINE
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您在 TransactionScope trans 范围之外创建了 table1Adapter。
Because you created the table1Adapter outside the TransactionScope trans scope.