/// <summary> /// Gets all the data from the tables provided. /// </summary> /// <param name="tableName"> The name of the table to recieve data from.</param> public static void getAllData(String tableName) { String sql = "SELECT * FROM " + tableName; SqlCommand command = new SqlCommand(sql, sc); SqlDataReader dataReader = command.ExecuteReader(); Animals.Clear(); AnimalReports.Clear(); if (tableName == "Animal") { while (dataReader.Read()) { Animal a1 = new Animal((int)dataReader.GetValue(0), (String)dataReader.GetValue(1), (int)dataReader.GetValue(2), (String)dataReader.GetValue(3), (int)dataReader.GetValue(4), (DateTime)dataReader.GetValue(5)); Animals.Add(a1); } dataReader.Close(); String sql2 = "SELECT * FROM AnimalReport"; SqlCommand command2 = new SqlCommand(sql2, sc); SqlDataReader dataReader2 = command2.ExecuteReader(); while (dataReader2.Read()) { AnimalReport ar1 = new AnimalReport((int)dataReader2.GetValue(0), (int)dataReader2.GetValue(1), (String)dataReader2.GetValue(2), (DateTime)dataReader2.GetValue(3)); AnimalReports.Add(ar1); } } Zoos.Clear(); ZooReports.Clear(); if (tableName == "Zoo") { while (dataReader.Read()) { Zoo z1 = new Zoo((int)dataReader.GetValue(0), (String)dataReader.GetValue(1), (String)dataReader.GetValue(2)); Zoos.Add(z1); } dataReader.Close(); String sql2 = "SELECT * FROM ZooReport"; SqlCommand command2 = new SqlCommand(sql2, sc); SqlDataReader dataReader2 = command2.ExecuteReader(); while (dataReader2.Read()) { ZooReport zr1 = new ZooReport((int)dataReader2.GetValue(0), (int)dataReader2.GetValue(1), (String)dataReader2.GetValue(2), (DateTime)dataReader2.GetValue(3)); ZooReports.Add(zr1); } } dataReader.Close(); }
private void Button_Click_1(object sender, RoutedEventArgs e) { ZooReport selectedReport = (ZooReport)reportcombobox.SelectedItem; if (selectedReport == null) { userviewbox.Text = "No report for such ID."; } else { userviewbox.Text = selectedReport.Description; } }
/// <summary> /// Method which adds a new zoo report to the database. /// </summary> /// <param name="zooId">The ID of the zoo being reported.</param> /// <param name="desc">The report body.</param> public static Boolean AddZooReport(int zooId, String desc) { Boolean result = false; ZooReport zr = new ZooReport(zooId, desc); String sql = "INSERT INTO ZooReport (ZooId, Description, Date) VALUES (@ZooId, @Description, @Date)"; using (SqlCommand command = new SqlCommand(sql, sc)) { command.Parameters.AddWithValue("@ZooId", zr.ZooId); command.Parameters.AddWithValue("@Description", zr.Description); command.Parameters.AddWithValue("@Date", zr.DateCreated); command.ExecuteNonQuery(); result = true; } return(result); }