在事务范围内打开一个sql连接重要吗 [英] Is it important to Open a sql connection in the transactionscope

查看:27
本文介绍了在事务范围内打开一个sql连接重要吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 sqlconnection,CN1.然后打开这个CN1.在代码的后面有一个事务范围.如果我在这个 CN1 连接上执行 sql 命令,这是在事务中吗?

I created a sqlconnection, CN1. Then this CN1 is opened. Later in the code there is a transactionscope. If I execute a sql command on this CN1 connection, is this within transaction?

代码如下所示;

SqlConnection cn1 = new SqlConnection();
cn1.Open(); //connection opened when there is no ambient transaction.
...

using(TransactionScope scope = new TransactionScope())
{
  SqlCommand cmd; //a typical sql command.

  ...

  cmd.ExecuteNonQuery(); //Is this command within transaction?
  ...
}

推荐答案

必须在 TransactionScope 内打开连接 以确保连接在事务中注册.

It is a MUST to open the connection within the TransactionScope to ensure that the connection is enrolled in the transaction.

这可以在 这个 MSDN 示例.

This is found in the comment just above the connection.Open in this MSDN example.

这篇关于在事务范围内打开一个sql连接重要吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆