Esempio n. 1
0
        /// <summary>
        /// Hàm cất 1 objec, dùng để lưu dữ liệu dạng bảng
        /// </summary>
        /// <param name="oEntity">Đối tượng cần cất</param>
        /// <param name="hasIdentity">=true Khóa là tự tăng, ngược lại không phải</param>
        /// <returns>Trả về số rowEffect khi cất đối tượng</returns>
        /// Create by: dvthang:19.04.2017
        public bool SaveDataTable(EntityBase oEntity)
        {
            bool isSuccess = false;
            int  rs        = -1;

            try
            {
                string storeName = String.Format(Commonkey.mscInsertUpdate, Commonkey.Schema, oEntity.GetType().Name);
                if (!string.IsNullOrEmpty(storeName))
                {
                    using (DbCommand command = this.DB.GetSqlStringCommand(storeName))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        this.DB.DiscoverParameters(command);
                        UtilityExtensions.MappingObjectIntoStore(command, oEntity);
                        if (m_ts != null)
                        {
                            rs        = this.DB.ExecuteNonQuery(command, m_ts);
                            isSuccess = rs > 0;
                        }
                        else
                        {
                            rs        = this.DB.ExecuteNonQuery(command);
                            isSuccess = rs > 0;
                        }
                    }
                }
                else
                {
                    throw new Exception($"Không thấy store:{storeName}");
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(isSuccess);
        }
Esempio n. 2
0
        /// <summary>
        /// Hàm cất 1 object
        /// </summary>
        /// <param name="oEntity">Đối tượng cần cất</param>
        /// <param name="hasIdentity">=true Khóa là tự tăng, ngược lại không phải</param>
        /// <returns>Trả về số rowEffect khi cất đối tượng</returns>
        /// Create by: dvthang:19.04.2017
        public ResultData SaveData(EntityBase oEntity, bool hasIdentity = false)
        {
            ResultData resultData = new ResultData();
            int        rs         = -1;

            try
            {
                string storeName = UtilityExtensions.GetStoreByEntityState(oEntity);
                if (!string.IsNullOrEmpty(storeName))
                {
                    using (DbCommand command = this.DB.GetSqlStringCommand(storeName))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        this.DB.DiscoverParameters(command);
                        UtilityExtensions.MappingObjectIntoStore(command, oEntity);
                        if (m_ts != null)
                        {
                            if (hasIdentity)
                            {
                                rs = Convert.ToInt32(this.DB.ExecuteScalar(command, m_ts));
                                resultData.PKValue = rs;
                            }
                            else
                            {
                                rs = this.DB.ExecuteNonQuery(command, m_ts);
                            }
                        }
                        else
                        {
                            if (hasIdentity)
                            {
                                rs = Convert.ToInt32(this.DB.ExecuteScalar(command));
                            }
                            else
                            {
                                rs = this.DB.ExecuteNonQuery(command);
                            }
                        }
                    }


                    if (rs > 0)
                    {
                        resultData.Success = true;
                        this.AfterSaveSuccess(oEntity);
                    }
                    else
                    {
                        resultData.Success = false;
                    }
                }
                else
                {
                    resultData.Success = false;
                    resultData.SetError(new Exception("Đối tượng không chưa gán EditMode."));
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                switch (ex.Number)
                {
                case 2627:      // Unique constraint error
                    break;

                case 547:       // Constraint check violation
                    break;

                case 2601:
                    //Duplicated key row error
                    break;
                }
                resultData.SetError(ex);
            }
            catch (Exception ex)
            {
                resultData.SetError(ex);
            }
            return(resultData);
        }