Search code examples
c#ms-accessoledb

Slow processing when dealing with an Access database


When reading and writing from the database, the device slows down exponentially and when the process repeats, it becomes much slower. Is there a way to speed up processing? I am writing a program by C # Windows Applications The code works properly and has no problems but the problem is only in the slow executable. As noted in the following code I take the student number from the first cell and then look in the database for the existence of a record of the student: If we find the student a record, the wizard will be directed to update student data If no student record is found, a new student record will be added to the student table This is a very slow code when it is re-executed:

prog1.Value = 0;
prog2.Value = 0;
prog1.Maximum = DGV1.RowCount;
string muoadNotSave = "";
prog2.Maximum = 7;
for (int i = 0; i < DGV1.RowCount; i += 5)
     {
      FlushMemory();//دالة تفريغ الذاكرة
      if (prog1.Value < DGV1.RowCount - 2)
         {
            prog1.Value += 5;
         }
         else
         {
          prog1.Value += 1;
         }
   try
        {

        if (DGV1.Rows[i].Cells[16].Value != null)
           { string name = DGV1.Rows[i].Cells[15].Value.ToString();
             int cell_no = DGV1.ColumnCount - 4;//عدد خلايا الصف
             for (int o = 0; o <= 6; o++)
                {
                 prog2.Value += 1;
                int shoabah_No = 0;
                int studenID =     
               int.Parse(DGV1.Rows[i].Cells[16].Value.ToString());
                        string ShoabahName =
               DGV1.Rows[i].Cells[cell_no].Value.ToString();
              OleDbConnection con77 = new 
              OleDbConnection(System.Configuration
             .ConfigurationManager.ConnectionStrings["acce"].ToString());
                        OleDbCommand com77 = new OleDbCommand();
                        com77.Connection = con77;
                        com77.CommandText = "SELECT * FROM Table_Shoab 
                       where shoba_name ='" + ShoabahName + "'";
                        con77.Open();
                        OleDbDataReader r77 = com77.ExecuteReader();
                        while (r77.Read())
                        {
                            shoabah_No = 
                          int.Parse(r77["shoba_id"].ToString());

                        }
                        con77.Close();

                        if (shoabah_No != 0)
                        {
             OleDbConnection con6 = new  
             OleDbConnection(System.Configuration
            .ConfigurationManager.ConnectionStrings["acce"].ToString());
                            OleDbCommand com6 = new OleDbCommand();
                            com6.Connection = con6;

                            com6.CommandText = "insert into 
                            link_stud_shobah (shoba_id,JLOS_NO) values 
                            ('" + shoabah_No + "','" + studenID + "')";
                            con6.Open();
                            com6.ExecuteNonQuery();
                            if (con6.State == ConnectionState.Open)
                                con6.Close();

                        }
                        else
                        {
                            muoadNotSave += 
                            DGV1.Rows[i].Cells[cell_no].Value.ToString()           
                            + "  " + studenID + "\n\r";
                        }


                        cell_no += -2;
                    }
                    prog2.Value = 0;



            }
                 }

                catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
         MessageBox.Show("اما ان يكون هناك مشكلة في الاتصال او ان الطالب " 
            + " " + DGV1.Rows[i].Cells[23].Value + " " + " سبق تسجيلة ", 
                  "تنبيه");
                // MessageBox.Show("" + ex, "تنبيه");
            }
            finally
            {

            }
        }
        if (muoadNotSave != "")
        {
            MessageBox.Show("الشعب التي لم تحفظ  "+muoadNotSave, 
          "تنبيه");
        }
        else
        {           
          MessageBox.Show("تم حفظ كافة الشعب بنجاح ولله الحمد   " + 
         muoadNotSave, "تنبيه");
        }

Solution

  • You are creating, opening and closing the OleDbConnection inside your (nested) loop, which is expensive.

    Try to do the following outside of the outer loop:

    OleDbConnection con77 = new OleDbConnection ..
    OleDbCommand com77 = new OleDbCommand();
    com77.Connection = con77;
    con77.Open();
    ..
    con77.Close();
    
    OleDbConnection con6 = new OleDbConnection ..
    OleDbCommand com6 = new OleDbCommand();
    com6.Connection = con6;
    con6.Open();
    ..
    con6.Close();
    

    Also, if both connections are the same you have to create only one.

    Not sure what FlushMemory() does, but this could also be expensive and it might be better to call this function after the main loop, or not at all.