.net – 在transactionscope中打开sql连接很重要

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

代码看起来像这样;

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中打开连接,以确保连接在事务中注册.

这可以在连接上方的注释中找到.在this MSDN示例中打开.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...