admin管理员组

文章数量:1122832

In my project whenever I press any one of the two buttons I've programmed (Add button to add items and update button to see the table in DataGridView), visual studio 2022 gets a sudden increase in memory usage from 20 MB to 27 MB and no matter how much I try the other button stops working, what should I do?

here's my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySQL.Data.EntityFrameworkCore;

namespace stok
{

    public partial class Form1 : Form
    {
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private Category category;


        public Form1()
        {
            InitializeComponent();
            category = new Category();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                category.CategoryId = Convert.ToInt32(textBox2.Text);
                category.CategoryName = textBox1.Text;

                category.UpdateCategory();
                MessageBox.Show("Category updated successfully!");
                ListCategories();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
            ListCategories();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "Kategoriler") 
            { 
                try
                {
                    if (string.IsNullOrEmpty(textBox1.Text))
                    {
                        MessageBox.Show("Please fill in the Category Name.");
                        return;
                    }

                    category.CategoryName = textBox1.Text;
                    category.CategoryId = Convert.ToInt32(textBox2.Text);
                    category.AddCategory();
                    MessageBox.Show("Category added successfully!");
                    ListCategories();



                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message);
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "Kategoriler")
            {
                try
                {
                    if (string.IsNullOrEmpty(textBox2.Text))
                    {
                        MessageBox.Show("Please fill in the Category ID.");
                        return;
                    }

                    category.CategoryName =(textBox1.Text);
                    category.DeleteCategory();
                    MessageBox.Show("row deleted successfully!");
                    ListCategories();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message);
                }
            }
            
        }
        private void button4_Click_1(object sender, EventArgs e)
        {
            ListCategories();
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // Optionally implement cell click handling logic
        }

        private void ListCategories()
        {
            try
            {
                DataTable dataTable = category.ListCategories();
                dataGridView1.DataSource = dataTable;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }
        internal class VeriTabaniBaglanti
        {
            string baglancumle = ConfigurationManager.ConnectionStrings["StokBaglantiCumlesi"].ConnectionString;

            public MySqlConnection baglan()
            {
                MySqlConnection baglanti = new MySqlConnection(baglancumle);
                MySqlConnection.ClearPool(baglanti);

                return baglanti;
            }
        }


        public class Category
        {
            public int CategoryId { get; set; }
            public string CategoryName { get; set; }

            private readonly VeriTabaniBaglanti databaseConnection;
            private readonly MySqlConnection connection;
            private readonly MySqlCommand command;

            public Category()
            {
                databaseConnection = new VeriTabaniBaglanti();
                connection = databaseConnection.baglan();
                command = new MySqlCommand { Connection = connection };
            }
            public DataTable ListCategories()
            {
                DataTable dataTable = new DataTable();

                try
                {
                    using (connection)
                    {
                        connection.Open();
                        command.CommandText = "SELECT * FROM kategoriler";
                        MySqlDataAdapter adapter = new MySqlDataAdapter(command);
                        DataTable kisilistesi = new DataTable();
                        adapter.Fill(kisilistesi);
                        return kisilistesi;
                        connection.Close();

                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }

                return dataTable;
            }
            public void AddCategory()
            {
                try
                {
                    using (connection)
                    {
                        connection.Open();
                        command.CommandText = "INSERT INTO kategoriler (kategori_adi) VALUES (@CategoryName)";
                        command.Parameters.Clear();
                        command.Parameters.AddWithValue("@CategoryName", CategoryName);
                        command.ExecuteNonQuery();
                        connection.Close();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }
            }

            public void UpdateCategory()
            {
                try
                {
                    using (connection)
                    {
                        connection.Open();
                        command.CommandText = "UPDATE kategoriler SET kategori_adi = @CategoryName WHERE kategori_id = @CategoryId";
                        command.Parameters.Clear();
                        command.Parameters.AddWithValue("@CategoryName", CategoryName);
                        command.Parameters.AddWithValue("@CategoryId", CategoryId);
                        command.ExecuteNonQuery();
                        connection.Close();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }
            }

            public void DeleteCategory()
            {
                try
                {
                    if (connection.State != ConnectionState.Open)
                    {
                        {
                            connection.Open();
                            command.CommandText = "DELETE FROM kategoriler WHERE kategori_id = @CategoryId";
                            command.Parameters.Clear();
                            command.Parameters.AddWithValue("@CategoryId", CategoryId);
                            command.ExecuteNonQuery();
                            connection.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }
            }


        }
        public class Tedarikci
        {
            public int Id { get; set; }
            public string FirmaAdi { get; set; }
            public string YetkiliAdSoyad { get; set; }
            public string Telefon { get; set; }
            public string Mail { get; set; }
            public string Adres { get; set; }

            private readonly VeriTabaniBaglanti databaseConnection;

            public Tedarikci()
            {
                databaseConnection = new VeriTabaniBaglanti();
            }

            public void Ekle()
            {
                using (var connection = databaseConnection.baglan())
                {
                    try
                    {
                        connection.Open();
                        using (var command = new MySqlCommand("INSERT INTO tedarikciler (firma_adi, yetkili_adsoyad, telefon, mail, adres) VALUES (@FirmaAdi, @YetkiliAdSoyad, @Telefon, @Mail, @Adres)", connection))
                        {
                            command.Parameters.AddWithValue("@FirmaAdi", FirmaAdi);
                            command.Parameters.AddWithValue("@YetkiliAdSoyad", YetkiliAdSoyad);
                            command.Parameters.AddWithValue("@Telefon", Telefon);
                            command.Parameters.AddWithValue("@Mail", Mail);
                            command.Parameters.AddWithValue("@Adres", Adres);
                            command.ExecuteNonQuery();
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Hata: " + ex.Message);
                        throw;
                    }
                }
            }

            public void Guncelle()
            {
                using (var connection = databaseConnection.baglan())
                {
                    try
                    {
                        connection.Open();
                        using (var command = new MySqlCommand("UPDATE tedarikciler SET firma_adi = @FirmaAdi, yetkili_adsoyad = @YetkiliAdSoyad, telefon = @Telefon, mail = @Mail, adres = @Adres WHERE id = @Id", connection))
                        {
                            command.Parameters.AddWithValue("@FirmaAdi", FirmaAdi);
                            command.Parameters.AddWithValue("@YetkiliAdSoyad", YetkiliAdSoyad);
                            command.Parameters.AddWithValue("@Telefon", Telefon);
                            command.Parameters.AddWithValue("@Mail", Mail);
                            command.Parameters.AddWithValue("@Adres", Adres);
                            command.Parameters.AddWithValue("@Id", Id);
                            command.ExecuteNonQuery();
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Hata: " + ex.Message);
                        throw;
                    }
                }
            }

            public void Sil()
            {
                using (var connection = databaseConnection.baglan())
                {
                    try
                    {
                        connection.Open();
                        using (var command = new MySqlCommand("DELETE FROM tedarikciler WHERE id = @Id", connection))
                        {
                            command.Parameters.AddWithValue("@Id", Id);
                            command.ExecuteNonQuery();
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Hata: " + ex.Message);
                        throw;
                    }
                }
            }

            public DataTable Listele()
            {
                using (var connection = databaseConnection.baglan())
                {
                    try
                    {
                        connection.Open();
                        using (var command = new MySqlCommand("SELECT * FROM tedarikciler", connection))
                        {
                            using (var adapter = new MySqlDataAdapter(command))
                            {
                                DataTable dataTable = new DataTable();
                                adapter.Fill(dataTable);
                                return dataTable;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Hata: " + ex.Message);
                        throw;
                    }
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

I've thought that maybe the reason is that I'm using two buttons and then tried doing both of the functions in a singular button, which didn't work. I tried changing the build from "Any CPU" to x64 (my OS is an x64 OS). I've also tried to close everything in my computer as to give visual studio more space in the memory and cpu, but that didn't do anything either. No matter what I do once the memory process goes over 25 it doesn't let me press anything

本文标签: