public CategoryEntity GetCategory(int id) { CategoryEntity c = null; using (var conn = new SqlConnection(_connectionString)) { var cmd = new SqlCommand(string.Format(Resources.DB_SQL_CAL_SEL, Resources.DB_COL_CAT_NAME + ", " + Resources.DB_COL_CAT_DESC, Resources.DB_TAB_CAT, Resources.DB_COL_CAT_ID, Resources.DB_COL_CAT_ID), conn); cmd.Parameters.AddWithValue("@" + Resources.DB_COL_CAT_ID, id); conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { reader.Read(); c = new CategoryEntity { CategoryID = id, CategoryName = (string) reader[Resources.DB_COL_CAT_NAME], Description = (string) reader[Resources.DB_COL_CAT_DESC] }; } } return c; }
private Category TranslateCategoryEntityToCategoryContractData( CategoryEntity categoryEntity) { return new Category { ID = categoryEntity.CategoryID, Name = categoryEntity.CategoryName, Description = categoryEntity.Description }; }
public bool UpdateCategory(CategoryEntity category) { using (var conn = new SqlConnection(_connectionString)) { var cmd = new SqlCommand( string.Format(Resources.DB_SQL_CAL_UPD, Resources.DB_TAB_CAT, Resources.DB_COL_CAT_NAME, Resources.DB_COL_CAT_NAME, Resources.DB_COL_CAT_DESC, Resources.DB_COL_CAT_DESC, Resources.DB_COL_CAT_ID, Resources.DB_COL_CAT_ID), conn); cmd.Parameters.AddWithValue("@" + Resources.DB_COL_CAT_NAME, category.CategoryName); cmd.Parameters.AddWithValue("@" + Resources.DB_COL_CAT_DESC, category.Description); cmd.Parameters.AddWithValue("@" + Resources.DB_COL_CAT_ID, category.CategoryID); conn.Open(); int numRows = cmd.ExecuteNonQuery(); return (numRows == 1); } }
public void UpdateCategory(CategoryEntity category) { if (category == null) { throw new ArgumentNullException("category"); } if (string.IsNullOrEmpty(category.CategoryName)) { throw new NoNullAllowedException(Resources.MSG_NULL_CAT_NAME); } if (string.IsNullOrEmpty(category.Description)) { throw new NoNullAllowedException(Resources.MSG_NULL_CAT_DESC); } if (!_categoryDAO.UpdateCategory(category)) { throw new DataException(Resources.MSG_UPDATE_FAILED); } }