Click here to Skip to main content
15,888,170 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
 -1
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:


What I have tried:

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, "تنبيه");
        }
Posted
Updated 19-May-18 1:45am
v2

Quote:
the device slows down exponentially and when the process repeats, it becomes much slower.

Can you define "slows down exponentially" and "much slower" ?
C#
com2.CommandText = "insert into sch_school1 (schoolnumd ,schoolname,Sys  ) values ('" + TBSchoolNum.Text + "','" + TBSchoolName.Text + "','" + TBschoolSys.Text + "')";
...
com2.CommandText = "update sch_school1  set  schoolname = '" + TBSchoolName.Text + "' where  schoolnumd ='" + TBSchoolNum.Text + "' ";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
v2
Comments
Ali Alshihry 19-May-18 7:59am    
Note / I modified the code in question and put the most slow code
Thanks for the advice
I will change the data entry so that there is no injection.
I mean slow
When you click the button for the first time, execution is unexpectedly slow
When the click is repeated, the execution becomes slower
When it is repeated a third time, it becomes semi-stopped and recording 200 records takes about an hour.
Patrice T 19-May-18 12:54pm    
In order to understand where and how time is spent, you need to time your code with a profiler tool.
Profiling (computer programming) - Wikipedia[^]
List of performance analysis tools - Wikipedia[^]
In addition to what ppolymoprphe has said - which is really important, and you need to do something about it through your whole app as a matter of urgency - your SQL is poor, and that doesn't help performance.
Why this:
C#
com1.CommandText = "SELECT  schoolname FROM  sch_school1 where schoolnumd='" + TBSchoolNum.Text + "'";
string schoolname = com1.CommandText;
con1.Open();
OleDbDataReader r1 = com1.ExecuteReader();
int y = 0;
while (r1.Read())
{ y += 1; }
con1.Close();
When you can do that without returning any rows, or creating a DataReader?
C#
com1.CommandText = "SELECT  COUNT(schoolname) FROM  sch_school1 where schoolnumd=@SN";
com1.Parameters.AddWithValue("@SN", TBSchoolNum.Text);
con1.Open();
int y = com1.ExecuteScalar():
con1.Close();
In fact, you don't even need to do that: you can just issue the UPDATE command, and check the return value (which is the number of rows affected) for zero. If no rows were updated, you need to do the INSERT.
 
Share this answer
 
Comments
Ali Alshihry 19-May-18 8:08am    
Thank you from the heart for your advice and I will do what you said
I actually had to update the records instead of searching and then editing and if I did not modify any record I would add well
OriginalGriff 19-May-18 8:13am    
You're welcome!
Just make sure you fix the SQL Injection throughout your app: leave one concatenation in, and you are still at risk!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900