简体   繁体   中英

C# Creating new instance or Overwrite?

I'm creating a C# Winform Application with SQL Server Database, in any form I declare a SqlCOnnection and a SqlCommand within the Form Class , but I start initializing in every method when needed. and here is my code :

    public partial class DrinkIncomeForm : Form
    {

        #region Class Variables
        private string conString;
        private string queryP1;
        private string queryP2;
        private SqlConnection con;
        private SqlCommand cmd;
        private SqlDataAdapter myAdapter;
        private SqlDataReader myReader;
        private DataTable drinksIncomeTable;
        #endregion

        public DrinkIncomeForm()
        {
            InitializeComponent();
        }
         private void DrinkIncomeForm_Load(object sender, EventArgs e)
        {
            conString = System.Configuration.ConfigurationManager.ConnectionStrings["MyGymConString"].ConnectionString;
            LoadDrinksCombo();
            LoadCashierCombo();
            DrinkComBox.SelectedIndex = 0;
            CashierComBox.SelectedIndex = 0;
            LoadDrinksIncomeDGV();
        }
        private void DrinkComBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDrinksIncomeDGV();
        }

        private void CashierComBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDrinksIncomeDGV();
        }
        private void LoadDrinksIncomeDGV()
        {
            con = new SqlConnection(conString);
            cmd = new SqlCommand();
            cmd.Connection = con;
            queryP1 = "select TransID, DrinkName, Quantity, Price, Offer, format(TransDate, 'dd/MM/yyyy hh:mm:ss')as TransDate, Cashier  from tblDrinksIncome ";
            queryP2 = " where 1=1 ";
            try
            {
                con.Open();
                cmd.Parameters.Clear();
                if(DrinkComBox.SelectedIndex >0)
                {
                    queryP2 += " AND DrinkName=@drinkname";
                    cmd.Parameters.AddWithValue("@drinkname", DrinkComBox.SelectedItem.ToString());
                }
                if (CashierComBox.SelectedIndex >0)
                {
                    queryP2 += " AND Cashier=@cashier";
                    cmd.Parameters.AddWithValue("@cashier", CashierComBox.SelectedItem.ToString());
                }
                cmd.CommandText = "" + queryP1 + queryP2;
                myAdapter = new SqlDataAdapter(cmd);
                drinksIncomeTable = new DataTable();
                myAdapter.Fill(drinksIncomeTable);
                DrinksIncomeDGV.DataSource = drinksIncomeTable;
                con.Close();
            }
            catch
            {
                con.Close();
                MessageBox.Show("Database Error.");
            }
        }
    }

as u see in every time the user select from the ComboBoxes(Filters) new instances will be created. my question here is is this has bad effects on memory and performance , and if so what is the better way to do this ? thanks :)

It might have little downgrades on performance, but it is good design.

But you should not store your connection in a member variable. but only in local variable. Best would be a using(var s = new SqlConnection(..)) . This way you ensure the Connection is Disposed at the end of your method, and any reference to it should be lost, so it can be garbage collected. The same is valid for the SqlCommand.

So your only critical error, is not to call Dispose , you call only Close .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM