你的事物写得不对。
string sql = "update ...";
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["DSN"].ToString());
conn.Open();
SqlTransaction sqlTran = conn.BeginTransaction();
SqlCommand cmd = conn.CreateCommand();
cmd.Transaction = sqlTran;
try
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery(); sql = "update ...";
cmd.CommandText = sql;
cmd.ExecuteNonQuery(); sqlTran.Commit();
}
catch(Exception e1)
{
try
{
sqlTran.Rollback();
}
catch(SqlException ex)
{
throw ex;
}
throw e1;
}
finally
{
conn.Close();
}

解决方案 »

  1.   

    楼主想根据ExecuteNonQuery()来控制是否回滚
    请参考http://community.csdn.net/Expert/topic/2995/2995537.xml?temp=.2118952
      

  2.   

    Sorry没有看清楚
    楼主的问题AddOrganizeKPIListCmd()这是增加操作方法,为什么增加有两次记录了,在没有记录的情况下,可以用修改方法来操作,想了两天了,还没有解决,请指教
    ---什么意思?
      

  3.   

    SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;");
    myConnection.Open();// Start a local transaction.
    SqlTransaction myTrans = myConnection.BeginTransaction();// Enlist the command in the current transaction.
    SqlCommand myCommand = myConnection.CreateCommand();
    myCommand.Transaction = myTrans;try
    {
      myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
      myCommand.ExecuteNonQuery();
      myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
      myCommand.ExecuteNonQuery();
      myTrans.Commit();
      Console.WriteLine("Both records are written to database.");
    }
    catch(Exception e)
    {
      try
      {
        myTrans.Rollback();
      }
      catch (SqlException ex)
      {
        if (myTrans.Connection != null)
        {
          Console.WriteLine("An exception of type " + ex.GetType() +
                            " was encountered while attempting to roll back the transaction.");
        }
      }  Console.WriteLine("An exception of type " + e.GetType() +
                        "was encountered while inserting the data.");
      Console.WriteLine("Neither record was written to database.");
    }
    finally
    {
      myConnection.Close();
    }
      

  4.   

    就是加了两个table到数据表里面了。在没有任何记录的情况下,可以用修改记录,来增加一条,也就是说在执行事务之前,table已增加一次。
      

  5.   

    AddOrganizeKPI 是不是被执行了两次? 根据数据量大小,可以考虑string sqlstr +=" insert ; ";生成完sqlstr语句后,再执行事务控制,插入数据库
      

  6.   

    SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;");
    myConnection.Open();// Start a local transaction.
    SqlTransaction myTrans = myConnection.BeginTransaction();// Enlist the command in the current transaction.
    SqlCommand myCommand = myConnection.CreateCommand();
    myCommand.Transaction = myTrans;try
    {
      myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
      myCommand.ExecuteNonQuery();
      myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
      myCommand.ExecuteNonQuery();
      myTrans.Commit();
      Console.WriteLine("Both records are written to database.");
    }
    catch(Exception e)
    {
      try
      {
        myTrans.Rollback();
      }
      catch (SqlException ex)
      {
        if (myTrans.Connection != null)
        {
          Console.WriteLine("An exception of type " + ex.GetType() +
                            " was encountered while attempting to roll back the transaction.");
        }
      }  Console.WriteLine("An exception of type " + e.GetType() +
                        "was encountered while inserting the data.");
      Console.WriteLine("Neither record was written to database.");
    }
    finally
    {
      myConnection.Close();
    }
      

  7.   

    本人也在寻找方法中,不过建议你看看sqlhelper,楼主共同交流吧。
      

  8.   

    to:  caoit (lost)  行了吗?
    俺解决了,你将两个Sql语句写在一起,一并提交,这样就可以了。如下:
    public bool DelCourse(int pID)
    {
    wisdeconnection conn=new wisdeconnection();
    try
    {   
    conn.BeginTransaction();
    conn.RunSql("delete from courseware where id= "+pID
    +"delete from coursepop where courseid="+pID);
    conn.CommitTransaction();
    return true;
    }
    catch(Exception er)
    {
    conn.Rollback();
    throw new Exception(er.Message);
    }
    finally
    {
    conn.Dispose();
    }

    }