if(条件成立)
{
       try{
       A添加记录到数据库;
        B添加记录到数据库;
        C添加记录到文件;
           }}
=================================
问,如何同步操作A B C
当A B C 都 成功 则 成功
如果任何一过程遇到不可知错误,3部操作均不实现。 

解决方案 »

  1.   

    A和B可以用ADO.NET的Transaction, 文件的Transaction不好控制,不过你可以自己控制。写文件失败时把A和B添加的记录删掉。
      

  2.   

    using (SqlConnection connection =
                new SqlConnection(connectionString))
    {
        SqlCommand command = connection.CreateCommand();
        SqlTransaction transaction = null;
        
        try
        {
            // BeginTransaction() Requires Open Connection
            connection.Open();
            
            transaction = connection.BeginTransaction();
            
            // Assign Transaction to Command
            command.Transaction = transaction;
            
            // Execute 1st Command
            command.CommandText = "Insert ...";
            command.ExecuteNonQuery();
            
            // Execute 2nd Command
            command.CommandText = "Update...";
            command.ExecuteNonQuery();
            
            transaction.Commit();
        }
        catch
        {
            transaction.Rollback();
            throw;
        }
        finally
        {
            connection.Close();
        }
    }
    是这个意思吧?文件咋办啊?
      

  3.   

    这样是可以实现。
    可是我比较关心的是同步能否回滚,而不是删了A B,如果是UPdate怎么弄,更新的旧值可能我都不知道是什么了。
    又如何在更新回来。