Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ADO.NET事务期间使用Using()块_C#_Transactions_Ado.net - Fatal编程技术网

C# 在ADO.NET事务期间使用Using()块

C# 在ADO.NET事务期间使用Using()块,c#,transactions,ado.net,C#,Transactions,Ado.net,我需要做一个相对简单的事务,但是它会影响5-6个表,所以我阅读了一些关于如何做的教程,因为我以前从未使用过事务,并且我最终使用。。。像这样的块: SqlTransaction transaction = null; SqlConnection connection = null; string localConnString =System.Web.Configuration.WebConfigurationManager..; try { connection = new SqlConne

我需要做一个相对简单的事务,但是它会影响5-6个表,所以我阅读了一些关于如何做的教程,因为我以前从未使用过事务,并且我最终使用。。。像这样的块:

SqlTransaction transaction = null;
SqlConnection connection = null;
string localConnString =System.Web.Configuration.WebConfigurationManager..;
try
{
  connection = new SqlConnection(localConnString);
  connection.Open();

  string insertSomeValues = "some SQL INSERT query"..
  using (SqlCommand command = new SqlCommand(insertSomeValues, connection))
  {   
    ..
  }
  using(SqlCommand newCommand = new SqlCommand(otherQuery, connection))
  {
    ..
  }
  transaction.Commit();
}
catch
{
  transaction.Rollback();
}
finally
{
  connection.Close();
}
所以我希望你明白了。我认为这个实现存在的问题是,由于using块主要是一个try-catch块,如果在执行过程中的某个地方抛出异常,那么其他代码仍将被执行,更重要的是事务。Commit;也这使得事务变得毫无用处,因为当我想要一个事务全部或全无时,我会使用部分更改的数据库

由于我在这方面缺乏经验,我想知道怎样才能妥善处理这种情况。在其中一个示例中,有一个bool标志,指示当前操作如何进行以及在调用transaction.Commit之前;这里有一些支票:

if (!hasErrors)
{
  transaction.Commit();
}

但是,由于我在事务中包含了多个表,并且我有一个用于获取信息或检查某些内容的查询,所以使用标志的想法并不十分诱人。我正在考虑删除using块,这样,至少我认为每当抛出错误时,我直接转到主catch块时,应该会发生这种情况。所以有一些想法,但实际的处理方法是什么?

我修改了ur代码,包括所有命令的公共事务

SqlTransaction transaction = null;
SqlConnection connection = null;

string localConnString =System.Web.Configuration.WebConfigurationManager..;
try
{
 connection = new SqlConnection(localConnString);
 connection.Open();
 transaction=connection.BeginTransaction();
 string insertSomeValues = "some SQL INSERT query"..
 using (SqlCommand command = new SqlCommand(insertSomeValues, connection,transaction))
 {   
  ..
 }
 using(SqlCommand newCommand = new SqlCommand(otherQuery, connection,transaction))
 {
  ..
 }
 transaction.Commit();
  }
  catch
 {
  transaction.Rollback();
  }
  finally
  {
    connection.Close();
  }