事务在C#事务范围中中止

时间:2016-03-09 23:30:30

标签: c# asp.net transactionscope

我在google搜索关于事务范围时看到了很多例子。但是我试图这样做,所以在我的情况下它抛出异常。异常显示"事务中止" ..我看到了可能解决它显示关闭连接。但在这里我关闭它在finally块。任何人可以帮助解决这个问题。

已更新

上面的连接问题已解决。现在在db.ExecuteNonQuery(cmd)中抛出异常; section.Accroding我的知识,直到thrasaction完成这个doen't执行。

mycode的

  public bool NewsType(int Id)
    {

          bool status = false;
          DatabaseProviderFactory factory = new DatabaseProviderFactory();
          Database db = factory.Create("NewsCon");
          con.Open(); 

        using (TransactionScope transactionScope = new TransactionScope())
        {
            try
            {



                String query = @"DELETE FROM NewsType WHERE ID =: ID";
                cmd = db.GetSqlStringCommand(query);
                db.AddInParameter(cmd, "ID", DbType.Int32, Id);
                db.ExecuteNonQuery(cmd); //<-- Now it shows from the error here.But i think inside transaction scope this doesn't execute until transaction has completed.

                //Second step goes here

                status = true;

                transactionScope.Complete();
                transactionScope.Dispose();

            }

            catch (Exception ex)
            {

                 throw;
            }
            finally
            {
                con.Close();
            }

            return status;
        }

    }

0 个答案:

没有答案