public GetPrescriptionsResponse GetPrescriptions(int id) { GetPrescriptionsResponse response; using (var connection = new SqlConnection(_connectionString)) using (var command = new SqlCommand()) { connection.Open(); command.Connection = connection; command.CommandText = "select * from Prescription " + "where Prescription.IdPrescription = @Id;"; command.Parameters.AddWithValue("Id", id); command.Parameters.Clear(); using (var reader = command.ExecuteReader()) { if (!reader.HasRows) { throw new Exception($"No prescriptions found for IdPrescription: {id}"); } response = new GetPrescriptionsResponse { IdPrescription = int.Parse(reader["IdPrescription"].ToString()), Date = DateTime.Parse(reader["Date"].ToString()) }; } command.CommandText = "select * from Medicament " + "inner join Prescription_Medicament on " + "Prescription_Medicament.IdMedicament = Medicament.IdMedicament " + "where Prescription_Medicament.IdPrescription = @Id;"; command.Parameters.AddWithValue("Id", id); command.Parameters.Clear(); using (var reader = command.ExecuteReader()) { if (!reader.HasRows) { throw new Exception($"No medicaments found for IdPrescription: {id}"); } response.Medicaments = new List <MedicamentsResponse>(); while (reader.Read()) { var medicament = new MedicamentsResponse { IdMedicament = int.Parse(reader["IdMedicament"].ToString()), Dose = int.Parse(reader["Dose"].ToString()), Details = reader["Details"].ToString(), Name = reader["Name"].ToString(), Description = reader["Description"].ToString(), Type = reader["Type"].ToString() }; response.Medicaments.Add(medicament); } } } return(response); }
public List <GetPrescriptionsResponse> GetPrescriptions(int id) { using (var client = new SqlConnection(ConString)) using (var com = new SqlCommand()) { com.Connection = client; client.Open(); var resp = new List <GetPrescriptionsResponse>(); com.CommandText = "select dbo.Prescription.IdPrescription, Date, DueDate, dbo.Prescription.IdPatient, dbo.Prescription.IdDoctor, Dose, Details, Name, Description, Type from dbo.Prescription join dbo.Prescription_Medicament on dbo.Prescription_Medicament.IdPrescription = dbo.Prescription.IdPrescription join dbo.Medicament on dbo.Prescription_Medicament.IdMedicament = dbo.Medicament.IdMedicament where dbo.Prescription_Medicament.IdMedicament ='" + id + "' order by Date desc"; var dr = com.ExecuteReader(); if (!dr.Read()) { throw new ArgumentException("Improper Medicament Id"); } do { var pres = new GetPrescriptionsResponse(); pres.IdPrescription = (int)dr["IdPrescription"]; pres.IdPatient = (int)dr["IdPatient"]; pres.IdDoctor = (int)dr["IdDoctor"]; pres.Date = (DateTime)dr["Date"]; pres.DueDate = (DateTime)dr["DueDate"]; pres.Description = (string)dr["Description"]; pres.Type = (string)dr["Type"]; pres.Dose = (int)dr["Dose"]; pres.Name = (string)dr["Name"]; pres.Details = (string)dr["Details"]; resp.Add(pres); } while (dr.Read()); dr.Close(); return(resp); } }