/// <summary> /// Update dữ liệu thông qua Model /// -- Nguyễn Văn Thao - 29/9/2009 -- /// </summary> /// <param name="baseModel">Model</param> public void Update(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.SQLUpdate(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 (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 { 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); } }
public ProcessTransaction() { cnn = new SqlConnection(DBUtils.GetDBConnectionString()); }