C# >
我不确定您是否使用了糟糕的变量名,或者是否混淆了代码,但您确实应该在需要帮助的代码中添加有意义的变量名 我肯定您可以做更多的事情来优化代码,但我会注意重用SqlCommand,删除重复查询,并首先正确处理异常处理C# >,c#,C#,我不确定您是否使用了糟糕的变量名,或者是否混淆了代码,但您确实应该在需要帮助的代码中添加有意义的变量名 我肯定您可以做更多的事情来优化代码,但我会注意重用SqlCommand,删除重复查询,并首先正确处理异常处理 foreach(temp1中的字符串ri) { for(int index=0;index0”,ri,ssl[index].ToString()); 命令=新的SqlCommand(查询,con); 如果((int)command.ExecuteScalar()==1) { gh2++
foreach(temp1中的字符串ri)
{
for(int index=0;index0”,ri,ssl[index].ToString());
命令=新的SqlCommand(查询,con);
如果((int)command.ExecuteScalar()==1)
{
gh2++;
gh4++;
}
if(索引==(ssl.Count-1))
{
query=string.Format(“更新样本集N00={0},其中关键字='{1}',gh.ToString(),ri);
命令=新的SqlCommand(查询,con);
command.ExecuteNonQuery();
gh=0;
query=string.Format(“更新样本集N01={0},其中关键字='{1}',gh2.ToString(),ri);
命令=新的SqlCommand(查询,con);
command.ExecuteNonQuery();
gh2=0;
query=string.Format(“更新样本集N10={0},其中关键字='{1}',gh3.ToString(),ri);
命令=新的SqlCommand(查询,con);
command.ExecuteNonQuery();
gh3=0;
query=string.Format(“更新样本集N11={0},其中关键字='{1}',gh4.ToString(),ri);
命令=新的SqlCommand(查询,con);
command.ExecuteNonQuery();
gh4=0;
}
}
捕获(例外情况除外)
{
System.Diagnostics.Debug.Print(例如ToString());
}
}
}
SqlCommand=newsqlcommand(“从示例中选择关键字”,con);
SqlDataAdapter da=新的SqlDataAdapter(命令);
数据集ds=新数据集();
da.填写(ds,“样品”);
foreach(ds.Tables[0].行中的数据行dr)
{
string KeywordCell=dr[“关键字”].ToString();
如果(关键字单元格!=“y”)
{
如果(!li.Contains(关键字单元格))
{
li.Add(关键字单元格);
}
}
}
很抱歉,您的代码很难理解变量的命名方式。
为了可读性,我重构了您的查询,转而重用单个SqlCommand(如果将循环外的一个计算在内,则总共两个),并删除了额外的查询
我猜性能差与缺少对象重用、查询工作的复制有关,并且您可能会左右抛出异常,而try/catch会导致堆栈在每次抛出异常时都会展开!检查输出窗格中的调试信息,或在System.Diagnostics.debug.Print(例如ToString())行上设置断点代码>展开堆栈将占用执行时间。
我不确定您是否使用了糟糕的变量名,或者是否混淆了代码,但您确实应该在需要帮助的代码中添加有意义的变量名
我肯定您可以做更多的事情来优化代码,但我会注意重用SqlCommand,删除重复查询,并首先正确处理异常处理
foreach(temp1中的字符串ri)
{
for(int index=0;index0”,ri,ssl[index].ToString());
命令=新的SqlCommand(查询,con);
如果((int)command.ExecuteScalar()==1)
{
gh2++;
gh4++;
}
if(索引==(ssl.Count-1))
{
query=string.Format(“更新样本集N00={0},其中关键字='{1}',gh.ToString(),ri);
命令=新的SqlCommand(查询,con);
command.ExecuteNonQuery();
gh=0;
query=string.Format(“更新样本集N01={0},其中关键字='{1}',gh2.ToString(),ri);
命令=新的SqlCommand(查询,con);
command.ExecuteNonQuery();
gh2=0;
query=string.Format(“更新样本集N10={0},其中关键字='{1}',gh3.ToString(),ri);
命令=新的SqlCommand(查询,con);
command.ExecuteNonQuery();
gh3=0;
query=string.Format(“更新样本集N11={0},其中关键字='{1}',gh4.ToString(),ri);
命令=新的SqlCommand(查询,con);
command.ExecuteNonQuery();
gh4=0;
}
}
捕获(例外情况除外)
{
System.Diagnostics.Debug.Print(例如ToString());
}
}
}
SqlCommand=newsqlcommand(“从示例中选择关键字”,con);
SqlDataAdapter da=ne
foreach (string ri in temp1)
{
for (int a3 = 0; a3 < ssl.Count; a3++)
{
//for (int tn = 0; tn < tempNam.Count ; tn++)
//{
try
{
SqlCommand cmd5 = new SqlCommand("select count(*) from sample s inner join sample ss on ss.KeyWord='" + ri + "' and ss. " + ssl[a3].ToString() + "=0 and s.KeyWord='y' and s. " + ssl[a3].ToString()+ "=0", con);
int im = (int)cmd5.ExecuteScalar();
if (im == 1)
{
gh += 1;
}
SqlCommand cmd6 = new SqlCommand("select count(*) from sample s inner join sample ss on ss.KeyWord='" + ri + "' and ss. " + ssl[a3].ToString() + "=0 and s.KeyWord='y' and s. " + ssl[a3].ToString() + ">0", con);
int im1 = (int)cmd6.ExecuteScalar();
if (im1 == 1)
{
gh2 += 1;
}
SqlCommand cmd7 = new SqlCommand("select count(*) from sample s inner join sample ss on ss.KeyWord='" + ri + "' and ss. " + ssl[a3].ToString() + ">0 and s.KeyWord='y' and s. " + ssl[a3].ToString() + "=0", con);
int im2 = (int)cmd7.ExecuteScalar();
if (im2 == 1)
{
gh3 += 1;
}
SqlCommand cmd8 = new SqlCommand("select count(*) from sample s inner join sample ss on ss.KeyWord='" + ri + "' and ss. " + ssl[a3].ToString() + ">0 and s.KeyWord='y' and s. " + ssl[a3].ToString() + ">0", con);
int im3 = (int)cmd8.ExecuteScalar();
if (im3 == 1)
{
gh4 += 1;
}
if (a3 == (ssl.Count-1))
{
SqlCommand ins = new SqlCommand("update sample set N00=" + gh + " where KeyWord='" + ri + "'", con);
ins.ExecuteNonQuery();
gh = 0;
SqlCommand ins1 = new SqlCommand("update sample set N01=" + gh2 + " where KeyWord='" + ri + "'", con);
ins1.ExecuteNonQuery();
gh2 = 0;
SqlCommand ins2 = new SqlCommand("update sample set N10=" + gh3 + " where KeyWord='" + ri + "'", con);
ins2.ExecuteNonQuery();
gh3 = 0;
SqlCommand ins4 = new SqlCommand("update sample set N11=" + gh4 + " where KeyWord='" + ri + "'", con);
ins4.ExecuteNonQuery();
gh4 = 0;
}
}
catch (Exception ex)
{
}
// }
}
}
SqlCommand cmd1s = new SqlCommand("select KeyWord from sample", con);
SqlDataAdapter da = new SqlDataAdapter(cmd1s);
DataSet ds = new DataSet();
da.Fill(ds, "sample");
foreach (DataRow dr in ds.Tables[0].Rows)
{
string dd = dr["KeyWord"].ToString();
if (dd != "y")
{
if (!li.Contains(dd))
{
li.Add(dd);
}
}
}
foreach (string ri in temp1)
{
for (int index = 0; index < ssl.Count; index++)
{
try
{
SqlCommand command;
string query;
query = string.Format("select count(*) from sample s inner join sample ss on ss.KeyWord='{0}' and ss.{1}=0 and s.KeyWord='y' and s.{1}=0", ri, ssl[index].ToString());
command = new SqlCommand(query, con);
if ((int)command.ExecuteScalar() == 1)
{
gh++;
gh3++;
}
query = string.Format("select count(*) from sample s inner join sample ss on ss.KeyWord='{0}' and ss.{1}=0 and s.KeyWord='y' and s.{1}>0", ri, ssl[index].ToString());
command = new SqlCommand(query, con);
if ((int)command.ExecuteScalar() == 1)
{
gh2++;
gh4++;
}
if (index == (ssl.Count-1))
{
query = string.Format("update sample set N00={0} where KeyWord='{1}'", gh.ToString(), ri);
command = new SqlCommand(query, con);
command.ExecuteNonQuery();
gh = 0;
query = string.Format("update sample set N01={0} where KeyWord='{1}'", gh2.ToString(), ri);
command = new SqlCommand(query, con);
command.ExecuteNonQuery();
gh2 = 0;
query = string.Format("update sample set N10={0} where KeyWord='{1}'", gh3.ToString(), ri);
command = new SqlCommand(query, con);
command.ExecuteNonQuery();
gh3 = 0;
query = string.Format("update sample set N11={0} where KeyWord='{1}'", gh4.ToString(), ri);
command = new SqlCommand(query, con);
command.ExecuteNonQuery();
gh4 = 0;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print(ex.ToString());
}
}
}
SqlCommand command = new SqlCommand("select KeyWord from sample", con);
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds, "sample");
foreach (DataRow dr in ds.Tables[0].Rows)
{
string KeywordCell = dr["KeyWord"].ToString();
if (KeywordCell != "y")
{
if (!li.Contains(KeywordCell))
{
li.Add(KeywordCell);
}
}
}