コード例 #1
0
        /// <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();
        }
コード例 #2
0
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            AnimalReport selectedReport = (AnimalReport)reportcombobox.SelectedItem;

            if (selectedReport == null)
            {
                userviewbox.Text = "No report for such ID.";
            }
            else
            {
                userviewbox.Text = selectedReport.Description;
            }
        }
コード例 #3
0
        /// <summary>
        /// Method which adds a new animal report to the database.
        /// </summary>
        /// <param name="animalId">The ID of the animal being reported.</param>
        /// <param name="desc">The report body.</param>
        public static Boolean AddAnimalReport(int animalId, String desc)
        {
            Boolean      result = false;
            AnimalReport ar     = new AnimalReport(animalId, desc);

            String sql = "INSERT INTO AnimalReport (AnimalId, Description, Date) VALUES (@AnimalId, @Description, @Date)";

            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@AnimalId", ar.AnimalId);
                command.Parameters.AddWithValue("@Description", ar.Description);
                command.Parameters.AddWithValue("@Date", ar.DateCreated);

                command.ExecuteNonQuery();
                result = true;
            }
            return(result);
        }