我有一些代码,它的功能类似于TransactionScope的建议使用,但具有环境连接而不是环境事务。
是否可以使用现有连接对象与TransactionScope对象配合使用,或者在.NET框架中有其他替代方案可供使用?
我有一些代码,它的功能类似于TransactionScope的建议使用,但具有环境连接而不是环境事务。
是否可以使用现有连接对象与TransactionScope对象配合使用,或者在.NET框架中有其他替代方案可供使用?
实际上,有一种方法。
connection.EnlistTransaction(Transaction.Current)
它能正常工作并且仅在必要时才会将事务推广到分布式,与文档所说的相反。
希望这有帮助。
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;
// Start a local transaction.
transaction = connection.BeginTransaction("SampleTransaction");
// Must assign both transaction object and connection
// to Command object for a pending local transaction
command.Connection = connection;
command.Transaction = transaction;
...
...
}