Example #1
0
        /// <summary>
        /// 执行Sql事物
        /// </summary>
        /// <param name="SqlList">sql集合</param>
        /// <param name="IsError">遇到错误是否继续执行</param>
        /// <param name="Exe">回掉函数</param>
        /// <returns>影响行数</returns>
        public static int Execute_Transaction(System.Collections.ArrayList SqlList, bool IsError, Executioned Exe)
        {
            bool ConnectState = false;

            ConnectState = ConnectSql();
            SqlErrorText = ConnectState ? string.Empty:"连接SqlServerError";
            if (!ConnectState)
            {
                return(0);
            }
            SqlTransaction oraTact = SqlHand.BeginTransaction();
            SqlCommand     oraCmd  = new SqlCommand();

            oraCmd.Connection     = SqlHand;
            oraCmd.Transaction    = oraTact;
            oraCmd.CommandTimeout = 1000;
            SqlNowCount           = 0;
            int OkCount = 0;

            foreach (string SqlStr in SqlList)
            {
                oraCmd.CommandText = SqlStr;
                bool State = true;
                try
                {
                    oraCmd.ExecuteNonQuery();
                    SqlErrorText = string.Empty;
                    OkCount     += 1;
                }
                catch (Exception ex)
                {
                    State        = false;
                    SqlErrorText = ex.Message;
                    if (!IsError)
                    {
                        oraTact.Rollback(); return(0);
                    }
                }
                SqlNowCount += 1;
                if (Exe != null)
                {
                    Exe(new ExecutionedReturn(State, SqlErrorText, SqlStr, SqlList.Count, SqlNowCount));
                }
            }
            oraTact.Commit();
            return(OkCount);
        }
Example #2
0
 /// <summary>
 /// 关闭SqlConnect
 /// </summary>
 /// <returns></returns>
 public static bool StopSql()
 {
     if (SqlHand == null)
     {
         SqlErrorText = string.Empty;
         return(true);
     }
     if (SqlHand.State != System.Data.ConnectionState.Open)
     {
         SqlErrorText = string.Empty;
         return(true);
     }
     try
     {
         SqlHand.Close();
         SqlErrorText = string.Empty;
         return(true);
     }
     catch (Exception ex)
     {
         SqlErrorText = ex.Message;
         return(false);
     }
 }