Ejemplo n.º 1
0
 /// <summary>
 /// 批量删除(事务)
 /// </summary>
 /// <typeparam name="T">实体类型</typeparam>
 /// <param name="ids">主键集合</param>
 /// <returns></returns>
 public Enums.TickLingEnum DeleteModelsTransaction <T>(string[] ids)
 {
     using (DbConnection Connection = new DBFactory().GetInstance())
     {
         Connection.Open();
         IDbTransaction transaction = Connection.BeginTransaction();
         try
         {
             Type   typeFromHandle    = typeof(T);
             string strTableName      = typeFromHandle.Name;
             string PrimaryKeyName    = string.Empty;
             List <PropertyInfo> list = typeFromHandle.GetProperties().Where(p => p.IsDefined(typeof(PrimaryKeyAttribute), false) == true).ToList();
             if (list.Count == 1)
             {
                 PrimaryKeyName = list[0].Name;
                 foreach (var item in ids)
                 {
                     DeleteTransaction <T>(string.Format(" {0} = '{1}' ", PrimaryKeyName, item), transaction, Connection);
                 }
                 transaction.Commit();
                 return(Enums.TickLingEnum.Success);
             }
             else
             {
                 return(Enums.TickLingEnum.Fail);
             }
         }
         catch (Exception ex)
         {
             transaction.Rollback();
             return(Enums.TickLingEnum.Abnormity);
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 通过事务批量新增
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public Enums.TickLingEnum InsertModelsTransaction <T>(List <T> list)
        {
            Type          typeFromHandle = typeof(T);
            string        strTableName   = typeFromHandle.Name;
            StringBuilder sbColumns      = new StringBuilder();
            StringBuilder sbParameters   = new StringBuilder();

            using (DbConnection Connection = new DBFactory().GetInstance())
            {
                Connection.Open();
                IDbTransaction transaction = Connection.BeginTransaction();
                try
                {
                    foreach (var item in list)
                    {
                        InsertTransaction(item, transaction, Connection);
                    }
                    transaction.Commit();
                    return(Enums.TickLingEnum.Success);
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return(Enums.TickLingEnum.Fail);;
                }
            }
        }