private BatchDto Fetch(IDataReader data) { var batch = new BatchDto(); using (var dr = new SafeDataReader(data)) { if (dr.Read()) { batch.Num = dr.GetInt32("batch_num"); batch.BatchDate = !dr.IsDBNull("batch_date") ? dr.GetSmartDate("batch_date", true) : null; batch.PayDate = !dr.IsDBNull("pay_date") ? dr.GetSmartDate("pay_date", true) : null; batch.Amount = (Decimal?)dr.GetValue("batch_amount"); batch.JobNum = (int?)dr.GetValue("job_num"); batch.Description = !dr.IsDBNull("batch_dscr") ? dr.GetString("batch_dscr") : null; batch.Updated = !dr.IsDBNull("updated") ? dr.GetSmartDate("updated", true) : null; batch.ThankYouMessage1 = !dr.IsDBNull("thank_you_1") ? dr.GetString("thank_you_1") : null; batch.StudyTopic = !dr.IsDBNull("study_topic") ? dr.GetString("study_topic") : null; batch.ThankYouMessage2 = !dr.IsDBNull("thank_you_2") ? dr.GetString("thank_you_2") : null; batch.MarketingResearchMessage = !dr.IsDBNull("marketing_research_message") ? dr.GetString("marketing_research_message") : null; } FetchChildren(dr); } return batch; }
/// <summary> /// Updates in the database all changes made to the Batch object. /// </summary> /// <param name="batch">The Batch DTO.</param> /// <returns>The updated <see cref="BatchDto"/>.</returns> public BatchDto Update(BatchDto batch) { using (var ctx = ConnectionManager<SqlConnection>.GetManager("MyDatabase")) { using (var cmd = new SqlCommand("dbo.UpdateBatch", ctx.Connection)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@batch_num", batch.Num).DbType = DbType.Int32; cmd.Parameters.AddWithValue("@batch_date", batch.BatchDate.DBValue).DbType = DbType.DateTime; cmd.Parameters.AddWithValue("@pay_date", batch.PayDate.DBValue).DbType = DbType.DateTime; cmd.Parameters.AddWithValue("@batch_amount", batch.Amount == null ? (object)DBNull.Value : batch.Amount.Value).DbType = DbType.Decimal; cmd.Parameters.AddWithValue("@job_num", batch.JobNum == null ? (object)DBNull.Value : batch.JobNum.Value).DbType = DbType.Int32; cmd.Parameters.AddWithValue("@batch_dscr", batch.Description == null ? (object)DBNull.Value : batch.Description).DbType = DbType.String; cmd.Parameters.AddWithValue("@updated", batch.Updated.DBValue).DbType = DbType.DateTime; cmd.Parameters.AddWithValue("@thank_you_1", batch.ThankYouMessage1 == null ? (object)DBNull.Value : batch.ThankYouMessage1).DbType = DbType.String; cmd.Parameters.AddWithValue("@study_topic", batch.StudyTopic == null ? (object)DBNull.Value : batch.StudyTopic).DbType = DbType.String; cmd.Parameters.AddWithValue("@thank_you_2", batch.ThankYouMessage2 == null ? (object)DBNull.Value : batch.ThankYouMessage2).DbType = DbType.String; cmd.Parameters.AddWithValue("@marketing_research_message", batch.MarketingResearchMessage == null ? (object)DBNull.Value : batch.MarketingResearchMessage).DbType = DbType.String; var rowsAffected = cmd.ExecuteNonQuery(); if (rowsAffected == 0) throw new DataNotFoundException("Batch"); } } return batch; }