【发布时间】:2017-07-30 15:40:36
【问题描述】:
我知道在 .net C# 中使用“using”关键字和如下代码实现 SQL Server 事务的解决方案:
InsertDetails()
{
using (TransactionScope ts = new TransactionScope())
{
InsertName();//SQL functions to insert name into name table
Insertaddress();//SQL functions to insert address into address table
InsertPhoneNo();//SQL functions to insert phone number into contact table
ts.Complete();
}
}
例如,我希望将 sql server 事务作为参数传递给不同数据库查询的许多不同函数,而不需要 using 语句示例。
在调用代码路径中的所有函数后,我想进行调用以提交数据,如果出现问题,则执行回滚。
伪代码如下所示
InsertDetails()
{
var transaction = new Transaction();
var sqlcon = new SqlConnection();
InsertName(transaction, sqlcon);//SQL functions to insert name into name table
Insertaddress(transaction, sqlcon);//SQL functions to insert address into address table
InsertPhoneNo(transaction, sqlcon);//code to insert phone no into contact table
try
{
ts.commit();
}
catch(Exception ex)
{
ts.rollback();
}
}
【问题讨论】:
-
是什么阻止你这样做? (当然除了重新发明轮子)
-
那么 using 语法更安全,将事务传递给方法的目的是什么?它应该是透明的
标签: c# sql-server transactions