Ejemplo n.º 1
0
 private void btnChange_Click(object sender, EventArgs e) //Изменение данных при нажатии на кнопку изменить
 {
     using (SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-O3331SL\SQLEXPRESS;Initial Catalog=PharmPanaceya;Integrated Security=True"))
     {
         if (txtAmount.TextLength != 0 && txtCategories.TextLength != 0 && txtDiscription.TextLength != 0 && txtName.TextLength != 0 && txtPrice.TextLength != 0)
         {
             try
             {
                 conn.Open();
                 SqlCommand cmd   = conn.CreateCommand();
                 SqlCommand cmd_1 = conn.CreateCommand();
                 SqlCommand cmd_2 = conn.CreateCommand();
                 cmd_1.CommandText = "SELECT IDCategory FROM [Categories] WHERE Name = @name";
                 cmd_1.Parameters.AddWithValue(@"name", txtCategories.Text);
                 int typeEx = int.Parse(cmd_1.ExecuteScalar().ToString());
                 cmd.CommandText = "UPDATE [Medicines] SET  Name = @Name, IDCategory = @IDCategory, Description = @Description, Price = @Price, Amount = @Amount, Presence = @Presence WHERE IDMedicine   = @IDMedicine";
                 cmd.Parameters.AddWithValue(@"IDMedicine", MedId);
                 cmd.Parameters.AddWithValue(@"IDCategory", typeEx);
                 cmd.Parameters.AddWithValue(@"Name", txtName.Text);
                 cmd.Parameters.AddWithValue(@"Description", txtDiscription.Text);
                 cmd.Parameters.AddWithValue(@"Price", txtPrice.Text);
                 cmd.Parameters.AddWithValue(@"Amount", txtAmount.Text);
                 string how;
                 if (checkPresence.Checked == true)
                 {
                     how = "True";
                 }
                 else
                 {
                     how = "False";
                 }
                 cmd.Parameters.AddWithValue(@"Presence", how);
                 cmd.ExecuteNonQuery();
                 MessageBox.Show("Запись успешно изменена!");
                 Medicines medicines = new Medicines();
                 medicines.Show();
                 this.Hide();
                 GC.Collect();
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.ToString());
             }
             finally
             {
                 conn.Close();
             }
         }
         else
         {
             MessageBox.Show("Введите данные!");
         }
     }
 }
        private void btnAdd_Click(object sender, EventArgs e) //Добавление записи в БД при нажатии на копку добавить по заполнению необходимых полей
        {
            if (txtAmount.TextLength != 0 && txtCategories.TextLength != 0 && txtDiscription.TextLength != 0 && txtName.TextLength != 0 && txtPrice.TextLength != 0)
            {
                using (SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-O3331SL\SQLEXPRESS;Initial Catalog=PharmPanaceya;Integrated Security=True"))
                {
                    try
                    {
                        conn.Open();
                        SqlCommand cmd = conn.CreateCommand();
                        cmd.CommandText = "INSERT INTO [Medicines] VALUES (@IDCategory,@Name,@Description,@Price,@Amount,@Presence)";
                        cmd.Parameters.AddWithValue(@"IDCategory", type.type);
                        cmd.Parameters.AddWithValue(@"Name", txtName.Text);
                        cmd.Parameters.AddWithValue(@"Description", txtDiscription.Text);
                        cmd.Parameters.AddWithValue(@"Price", txtPrice.Text);
                        cmd.Parameters.AddWithValue(@"Amount", txtAmount.Text);
                        string how;
                        if (checkPresence.Checked == true)
                        {
                            how = "True";
                        }
                        else
                        {
                            how = "False";
                        }
                        cmd.Parameters.AddWithValue(@"Presence", how);
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("Запись успешно добавлена!");
                        Medicines medicines = new Medicines();
                        medicines.Show();
                        this.Hide();
                        GC.Collect();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }

            else
            {
                MessageBox.Show("Введите данные!");
            }
        }