Exemple #1
0
        /// <summary>
        /// 新增資料列
        /// </summary>
        /// <param name="rowdata"></param>
        /// <returns></returns>
        public bool Insert(T rowdata)
        {
            InsertCmd <T> cmd = new InsertCmd <T>(dbEntity);

            try
            {
                var str = cmd.GetInsertCmd(rowdata);
                if (string.IsNullOrEmpty(str))
                {
                    return(false);
                }
                try
                {
                    CommandDefinition cd = new CommandDefinition(commandText: str.ToString(), transaction: globalTrans, commandTimeout: _cmdTimeout);
                    cmd.Connection.Execute(cd);
                    return(true);
                }
                catch (Exception err)
                {
                    ErrLog.ExceptionLog(err, $"insert row into {rowdata.GetType().Name} occur error.");
                    throw err;
                }
                finally
                {
                    if (globalTrans == null)
                    {
                        cmd.Connection.Close();
                    }
                }
            }
            catch (Exception err)
            {
                throw err;
            }
        }
Exemple #2
0
        /// <summary>
        /// 新增資料集
        /// </summary>
        /// <param name="rowdata"></param>
        /// <returns></returns>
        public bool Insert(List <T> rowdata)
        {
            InsertCmd <T> cmd = new InsertCmd <T>(dbEntity);

            IDbTransaction trans = globalTrans ?? cmd.Connection.BeginTransaction();

            try
            {
                string str = "";
                foreach (T row in rowdata)
                {
                    str += cmd.GetInsertCmd(row);
                }
                if (string.IsNullOrEmpty(str))
                {
                    return(false);
                }
                try
                {
                    CommandDefinition cd = new CommandDefinition(commandText: str.ToString(), transaction: trans, commandTimeout: _cmdTimeout);
                    cmd.Connection.Execute(cd);
                    if (globalTrans == null)
                    {
                        trans.Commit();
                    }
                    return(true);
                }
                catch (Exception err)
                {
                    ErrLog.ExceptionLog(err, $"insert rows to table {rowdata.GetType().Name} occur error.");
                    if (globalTrans == null)
                    {
                        trans.Rollback();
                    }
                    throw err;
                }
                finally
                {
                    if (globalTrans == null)
                    {
                        trans.Dispose();
                        cmd.Connection.Close();
                    }
                }
            }
            catch (Exception err)
            {
                ErrLog.ExceptionLog(err, $"insert commands occur error.");
                throw err;
            }
        }