protected void Button1_Click(object sender, EventArgs e)
        {
            // Vlozeni nove kategorie
            // bez validatoru
            DatabaseLibrary.Category cat = new DatabaseLibrary.Category();

            cat.category_name = TextBox_categoryName.Text;
            cat.category_type = Convert.ToInt32(TextBox_categoryType.Text);

            new DatabaseLibrary.CategoryTable().InsertCategory(cat);

            Response.Redirect(Request.RawUrl);
        }
        public void InsertCategory(Category cat)
        {
            using (MySqlConnection conn = new MySqlConnection(connString))
            {
                conn.Open();
                MySqlCommand command = new MySqlCommand(INSERT_CATEGORY, conn);

                /* Add parameters into the command */
                command.Parameters.AddWithValue("@category_name", cat.category_name);
                command.Parameters.AddWithValue("@category_type", cat.category_type);

                /* Executes the command */
                command.ExecuteNonQuery();
            }
        }
        public List<Category> SelectAll()
        {
            List<Category> catList = new List<Category>();
            using (MySqlConnection conn = new MySqlConnection(connString))
            {
                conn.Open();
                MySqlCommand command = new MySqlCommand(SELECT_ALL, conn);
                MySqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    Category cat = new Category();
                    cat.category_id = reader.GetInt32(0);
                    cat.category_name = reader.GetString(1);
                    cat.category_type = reader.GetInt32(2);

                    catList.Add(cat);
                }
            }
            return catList;
        }
        public void Update(Category cat)
        {
            using (MySqlConnection conn = new MySqlConnection(connString))
            {
                conn.Open();
                MySqlCommand command = new MySqlCommand(UPDATE_CATEGORY, conn);

                command.Parameters.AddWithValue("@category_id", cat.category_id);
                command.Parameters.AddWithValue("@category_name", cat.category_name);
                command.Parameters.AddWithValue("@category_type", cat.category_type);

                /* Executes the command */
                command.ExecuteNonQuery();
            }
        }
        public Category SelectOne(int categoryId)
        {
            Category cat = new Category();
            using (MySqlConnection conn = new MySqlConnection(connString))
            {
                conn.Open();
                MySqlCommand command = new MySqlCommand(SELECT_ONE, conn);
                command.Parameters.AddWithValue("@category_id", categoryId);

                MySqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    cat.category_id = reader.GetInt32(0);
                    cat.category_name = reader.GetString(1);
                    cat.category_type = reader.GetInt32(2);
                }
                reader.Close();
            }
            return cat;
        }
Exemple #6
0
        /// <summary>
        /// Tato trida slouzi pouze pro otestovani metod, ktere pracuji s databazi.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            /*
             * Otestovani tridy CategoryTable.
             */
            CategoryTable catTable = new CategoryTable();
            Category cat1, cat2;

            // Vlozeni dvou kategorii
            catTable.InsertCategory((cat1 = new Category("Vystava", 1)));
            catTable.InsertCategory((cat2 = new Category("Autogramiada", 2)));

            // Editace prvni kategorie
            cat1.category_name = "Prednaska";
            catTable.Update(cat1);

            // Vypis vsech kategorii
            List<Category> list = catTable.SelectAll();
            Console.WriteLine("Prehled kategorii:");
            foreach (Category cat in list)
                Console.WriteLine("Name: " + cat.category_name + " Type:" + cat.category_type.ToString());

            // Smazani kategorii
            foreach (Category cat in list)
                catTable.Delete(cat.category_id);

            /*
             * Otestovani tridy ClientTable.
             */
            //ClientTable clientTable = new ClientTable();

            // Vlozeni klienta
            //Client c1 = new Client();
            //c1.client_name = "Pepa";
            //c1.client_surname = "Z depa";
            //c1.client_email = "*****@*****.**";
            //c1.client_phone = "123456789";
            //c1.client_member_from = DateTime.Now;
            //c1.client_birth_date = DateTime.Now;
            //c1.client_isEmp = false;
            //c1.client_is_active = true;
            //c1.client_login = "******";
            //c1.client_pass_hash = "rnfguiNFUI1518";

            //clientTable.Insert(c1);

            // Vypis klientu
            //foreach (Client c in clientTable.SelectAll())
            //{
            //    c1 = c;
            //    Console.WriteLine(c.client_name + " " + c.client_surname + " " + c.client_email);
            //}

            //// Editace udaju
            //c1.client_street = "V depu 123";
            //clientTable.Update(c1);

            // Odstraneni clienta
            //clientTable.Delete(c1.client_id);

            /*
             * Otestovani tridy BorrowingTable
             */
            //BorrowingTable borrTable = new BorrowingTable();

            //Borrowing b = new Borrowing();
            //b.borrowing_from = DateTime.Now;
            //b.borrowing_to = DateTime.Now;
            //b.borrowing_is_returned = false;
            //b.client_id = 7;
            //b.copy_id = 1;

            //borrTable.Insert(b);

            // Vypsani vypujcek
            //foreach (Borrowing borr in borrTable.SelectAll())
            //{
            //    b = borr;
            //    Console.WriteLine(borr.borrowing_id.ToString() + " " + borr.borrowing_to);
            //}

            //// Editace vypujcky
            //b.borrowing_is_returned = true;
            //borrTable.Update(b);

            //// Odstraneni vypujcky
            //borrTable.Delete(b.borrowing_id);
        }