Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a for loop that I wish to optimize, someone can help me or give me some tricks to optimize

string sql1 = "select * from (select distinct left(start_bin_value,6) from sys_mast_inter_bin_table mast join cht_card_brand cb on mast.card_brand = cb.index_field join cht_country ctry on mast.bin_country = ctry.index_field join cht_service_type st on mast.service_type = st.index_field where cb.card_brand = '" + this.Card_brand + "' and ctry.client_country = '" + this.Bin_country + "' and st.service_type = '" + this.Service_type + "' ) where rownum <= " + this.TestCases + " order by dbms_random.value";

                       OleDbCommand command1 = new OleDbCommand(sql1, this.Connection);
                       OleDbDataReader reader1 = command1.ExecuteReader();
                       LuhnCheck lc = new LuhnCheck();

                       while (reader1.Read())
                       {
                           string result = lc.generate(reader1[0].ToString(), Convert.ToInt32(txtChangeLength.Text));
                           txtRandCardNumbers.Text += result + Environment.NewLine;
                       }
                       int rows = this.TestCases;
                       string[] textCardNumber = new string[rows];
                       StringReader readingCardNumber = new System.IO.StringReader(txtRandCardNumbers.Text);
                       int numberofrows = txtRandCardNumbers.Lines.Count() - 1;
                       if (reader1.Read() == false && numberofrows < this.TestCases)
                       {
                           int f = 0;
                           int loop = this.TestCases - txtRandCardNumbers.Lines.Count();

                           for (int cx = 0; cx <= loop; cx++)
                           {
                               textCardNumber[cx] = readingCardNumber.ReadLine();
                               if (textCardNumber[cx] is null)
                               {
                                   textCardNumber[cx] = textCardNumber[f + 0];
                                   f++;
                               }
                               txtRandCardNumbers.Text += textCardNumber[cx] + Environment.NewLine;

                           }
                       }
                       reader1.Close();
                   }


What I have tried:

I tried to surf the internet and I found out about parallel array but I don't know how I can do it.
Posted
Updated 26-Sep-17 13:00pm
Comments

C#
txtRandCardNumbers.Text += textCardNumber[cx] + Environment.NewLine;

I would use StringBuilder[^]
C#
var sb = new StringBuilder;

for (int cx = 0; cx <= loop; cx++)
{
    ...
    sb.AppendLine(textCardNumber[cx]);
    ...
}

txtRandCardNumbers.Text = sb.ToString();
 
Share this answer
 
Comments
Joe Doe234 26-Sep-17 6:31am    
thanks graeme , there is something else that i can change ? i just need to learn optimization
C#
string sql1 = "select * from (select distinct left(start_bin_value,6) from sys_mast_inter_bin_table mast join cht_card_brand cb on mast.card_brand = cb.index_field join cht_country ctry on mast.bin_country = ctry.index_field join cht_service_type st on mast.service_type = st.index_field where cb.card_brand = '" + this.Card_brand + "' and ctry.client_country = '" + this.Bin_country + "' and st.service_type = '" + this.Service_type + "' ) where rownum <= " + this.TestCases + " order by dbms_random.value";

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
 
Comments
Joe Doe234 27-Sep-17 3:40am    
and what can I do instead of ex: this.card_brand?

PS: These are all combo boxes and the 2 textboxes I have only accepts numbers.

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