public Section AddSection(Section c) { logger.InfoFormat("Adding section: {0} - {1}", c.SectionName, c.SectionDescription); if (c.SectionName.Length >= 50) c.SectionName = c.SectionName.Substring(0, 49); if (c.SectionDescription.Length >= 255) c.SectionDescription = c.SectionDescription.Substring(0, 254); DateTime addDate = DateTime.UtcNow; string addedBy = HttpContext.Current.Request.LogonUserIdentity.Name; if (string.IsNullOrEmpty(addedBy)) { logger.Warn("Couldn't figure out HttpContext user identity. Using Environment.UserName instead"); addedBy = Environment.UserName; } using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)) { conn.Open(); try { var result = conn.Query<int>(@" insert into amz.Sections (SectionName , SectionDescription , AddDate , AddedBy ) values(@SectionName , @SectionDescription , @addDate , @addedBy ) SELECT SCOPE_IDENTITY() ", new { c.SectionName , c.SectionDescription , addDate , addedBy }); if (result.Any()) { int insertedSectionId = (int)result.ElementAtOrDefault(0); return GetSection(insertedSectionId); } } catch (Exception ex) { logger.Error(ex); } } return null; }
public bool DeleteSection(Section c) { bool result = true; logger.InfoFormat("Deleting section: {0} - {1}", c.SectionName,c.SectionDescription); DateTime updateDate = DateTime.UtcNow; string updatedBy = HttpContext.Current.Request.LogonUserIdentity.Name; if (string.IsNullOrEmpty(updatedBy)) { logger.Warn("Couldn't figure out HttpContext user identity. Using Environment.UserName instead"); updatedBy = Environment.UserName; } using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)) { conn.Open(); try { conn.Execute(@" delete amz.Sections where SectionID = @SectionID ", new { c.SectionID }); } catch (Exception ex) { result = false; logger.Error(ex); } } return result; }