zzl001 发表于 2018-10-23 09:55:36

SQL事务

public void ProTranandler()  
      {
  
            using (SqlConnection conn = new SqlConnection(this.CONN_LINK))
  
            {
  
                conn.Open();
  
                SqlTransaction tran = conn.BeginTransaction();
  
                SqlCommand scd = new SqlCommand();
  
                scd.Connection = conn;
  
                //--------------------------------------------------------------
  
                //--------------调用"存储过程"
  
                //scd.CommandText = "ProSql";
  
                //scd.CommandType = CommandType.StoredProcedure;
  
                //SqlParameter sqtIN = new SqlParameter("@inParam", SqlDbType.Int);
  
                //sqtIN.Direction = ParameterDirection.Input;
  
                //sqtIN.Value = 1;
  
                //SqlParameter sqtOUT = new SqlParameter("@ok", SqlDbType.Int);
  
                //sqtOUT.Direction = ParameterDirection.Output;
  
                //scd.Parameters.Add(sqtIN);
  
                //scd.Parameters.Add(sqtOUT);
  
                try
  
                {
  
                  scd.Transaction = tran;
  
                  scd.CommandText = "SQL1";
  
                  scd.ExecuteNonQuery();
  
                  scd.CommandText = "SQL2";
  
                  scd.ExecuteNonQuery();
  
                  tran.Commit();
  
                }
  
                catch (Exception e)
  
                {
  
                  tran.Rollback();
  
                  Console.WriteLine(e.Message);
  
                }
  
                finally
  
                {
  
                  scd.Dispose();
  
                  conn.Close();
  
                }
  
            }
  
      }


页: [1]
查看完整版本: SQL事务