怎么保证他们要么都成功,要么都失败
--用事务来处理,失败一起RollBack如何知道更新数据库影响记录的数量?
--ExecuteNonQuery()

解决方案 »

  1.   

    /// <summary>
    /// 执行多个SQL语句
    /// </summary>
    /// <param name="sqlArray">动态数组:需要执行的多个SQL语句</param>
    public static bool RunMultiSQL(System.Collections.ArrayList sqlArray)
    {
    if(sqlArray.Count < 1) return true; bool success = false; SqlConnection tmpConn = Common.MySettings.MyConn;
    SqlCommand tmpCmm = new SqlCommand();
    tmpCmm.Connection = tmpConn;
    tmpConn.Open(); SqlTransaction tmpTrans = tmpConn.BeginTransaction();
    tmpCmm.Transaction = tmpTrans;
    try
    {
    for(int i=0;i<sqlArray.Count;i++)
    {
    tmpCmm.CommandText = sqlArray[i].ToString();
    tmpCmm.ExecuteNonQuery();
    } tmpTrans.Commit(); success = true;
    Common.ShowMsg("保存成功!");
    }
    catch(System.Exception ex)
    {
    tmpTrans.Rollback();
    Common.ShowMsg("保存失败!");
    Common.ShowMsg(ex.ToString());
    }
    finally
    {
    tmpTrans.Dispose();
    tmpConn.Close();
    }
    return success;
    }
      

  2.   

    上面给出的例子就是一个事务处理;
    至于执行一条更新受影响的记录数 int tmp = tmpCmm.ExecuteNonQuery();就可以了