使用了SqlHelper,如何让它回滚,如何取回返回值

类别:.NET开发 点击:0 评论:0 推荐:

   SqlConnection CS;
   CS = new SqlConnection(m_strConnectionString);
   
   SqlTransaction DBTransaction = null;
   string p_strReturnMessage = "";
   int p_intOutPut = 0;

   try
   {
    #region Populate Parameters
    System.Data.SqlClient.SqlParameter[] arParams = new System.Data.SqlClient.SqlParameter[4];

    arParams[0] = new System.Data.SqlClient.SqlParameter("@intOrganizeID", System.Data.SqlDbType.Int);
    arParams[1] = new System.Data.SqlClient.SqlParameter("@strMacAddress", System.Data.SqlDbType.VarChar, 256);
    arParams[2] = new System.Data.SqlClient.SqlParameter("@strMacCheck", System.Data.SqlDbType.VarChar, 256);
    arParams[3] = new System.Data.SqlClient.SqlParameter("@intOutPut", System.Data.SqlDbType.Int);

    arParams[3].Direction = System.Data.ParameterDirection.Output;

    arParams[0].Value = 1;
    arParams[1].Value = "111";
    arParams[2].Value = "111";
    arParams[3].Value = p_intOutPut;
    #endregion
    
    #region Execute SQL
    CS.Open();
    DBTransaction = CS.BeginTransaction();
    SqlHelper.ExecuteNonQuery(DBTransaction, CommandType.StoredProcedure, "pro_T_Accredit_Create", arParams);

    p_intOutPut = Convert.ToInt32(arParams[3].Value.ToString());
    #endregion

    #region Check Result
    if(p_intOutPut < 0)
    {
     DBTransaction.Rollback();        //--回退事务
    }
    else
    {
     DBTransaction.Commit();         //--提交事务
    }
    #endregion
   }
   catch(System.NullReferenceException NullEx)
   {
    DBTransaction.Rollback();         //--回退事务
   }
   catch(Exception ex)
   {
    DBTransaction.Rollback();         //--回退事务
   }

本文地址:http://com.8s8s.com/it/it43067.htm