优化C#中的for循环 [英] Optimizing a for loop in C#

查看:274
本文介绍了优化C#中的for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个我希望优化的for循环,有人可以帮助我或给我一些优化的技巧



字符串sql1 =select * from(从sys_mast_inter_bin_table中选择不同的左(start_bin_value,6))在mast.card_brand = cb.index_field加入cht_country ctry on上加入cht_card_brand cb mast.bin_country = ctry.index_field在mast.service_type = st.index_field上加入cht_service_type st,其中cb.card_brand ='+ this.Card_brand +'和ctry.client_country ='+ this.Bin_country +'和st.service_type ='+ this.Service_type +')其中rownum< =+ this.TestCases +按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]为null)
{
textCardNumber [cx] = textCardNumber [f + 0];
f ++;
}
txtRandCardNumbers.Text + = textCardNumber [cx] + Environment.NewLine;

}
}
reader1.Close();
}





我尝试过:



我试图在网上冲浪,我发现有关并行数组,但我不知道怎么做。

解决方案

< blockquote>

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



我会使用StringBuilder [ ^ ]

  var  sb =  new  StringBuilder; 

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

txtRandCardNumbers.Text = sb.ToString();


  string  sql1 =   select * from(从sys_mast_inter_bin_table中选择distinct left(start_bin_value,6)在mast.card_brand = cb.index_field上连接cht_card_brand cb在mast.bin_country = ctry.index_field上连接cht_country ctry在mast.service_type = st.index_field连接cht_service_type st其中cb.card_brand =' +  .Card_brand +  '和ctry.client_country =' +  .Bin_country +  '和st.service_type =' +  .Service_type +  ')其中rownum< = +  this  .TestCases +  按dbms_random.value排序; 



不是你问题的解决方案,而是你遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


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.

解决方案

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


I would use StringBuilder[^]

var sb = new StringBuilder;

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

txtRandCardNumbers.Text = sb.ToString();


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[^]


这篇关于优化C#中的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆