Exemplo n.º 1
0
        public void UpdateQLSX(BaseModel baseModel)
        {
            #region Get systemDate
            DateTime sysTime = GetSystemDate();
            #endregion

            #region Khai bao cac bien connection
            string TableName = baseModel.GetType().Name.Substring(0, baseModel.GetType().Name.Length - 5);
            string sql       = DBUtils.SQLUpdateQLSX(baseModel);
            cmd             = new SqlCommand(sql, cnn, tran);
            cmd.CommandType = CommandType.Text;
            PropertyInfo[] propertiesName = baseModel.GetType().GetProperties();
            object         value;
            #endregion

            #region Gan cac bien vao command goc
            for (int i = 0; i < propertiesName.Length; i++)
            {
                SqlDbType dbType = DBUtils.ConvertToSQLType(propertiesName[i].PropertyType);
                value = propertiesName[i].GetValue(baseModel, null);

                //if (propertiesName[i].Name.ToLower().Equals("updatedby"))
                //{
                //    cmd.Parameters.Add("@" + propertiesName[i].Name, SqlDbType.NVarChar).Value = !String.IsNullOrEmpty(Global.AppUserName) ? Global.AppUserName : (value ?? "");
                //}
                //else if (propertiesName[i].Name.ToLower().Equals("updateddate") || propertiesName[i].Name.ToLower().Equals("updatedate"))
                //{
                //    cmd.Parameters.Add("@" + propertiesName[i].Name, SqlDbType.DateTime).Value = sysTime;
                //}
                //else if (propertiesName[i].Name.ToLower().Equals("userupdateid"))
                //{
                //    cmd.Parameters.Add("@" + propertiesName[i].Name, SqlDbType.Int).Value = Global.UserID != 0 ? Global.UserID : (value ?? 0);
                //}
                //else if (value != null)
                if (value != null)
                {
                    if (propertiesName[i].PropertyType.Equals(typeof(DateTime)))
                    {
                        if ((DateTime)value == DateTime.MinValue)
                        {
                            value = DefValues.Sql_MinDate;
                        }
                    }
                    cmd.Parameters.Add("@" + propertiesName[i].Name, dbType).Value = value;
                }
                else
                {
                    if (propertiesName[i].PropertyType.Equals(typeof(DateTime?)))
                    {
                        cmd.Parameters.Add("@" + propertiesName[i].Name, dbType).Value = DBNull.Value;
                    }
                    else
                    {
                        cmd.Parameters.Add("@" + propertiesName[i].Name, dbType).Value = "";
                    }
                }
            }
            #endregion

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (SqlException se)
            {
                tran.Rollback();
                throw new Exception("Update " + baseModel.GetType().Name + " error :" + se.Message);
            }
        }
Exemplo n.º 2
0
 public ProcessTransaction()
 {
     cnn = new SqlConnection(DBUtils.GetDBConnectionString());
 }