下面的代码要花费几个小时才能执行。我正在比较数据库中的每个字符串以计算N00,N01,N10,N11参数。 Temp1是类型字符串列表,它由5000多个单词组成
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);
}
}
}
最佳答案
将索引放在Sample.KeyWord
上。这将使这变得快得多。
就像下面的安东尼的评论一样,这段代码有很多错误。该索引是我猜测花费最多的时间,但是您也应该阅读以下主题:
Evil Practices - Swallowing Exceptions
The SqlParameter Class
The using() construct-这适用于实现IDisposable
接口的任何对象。
.NET Naming Conventions and Programming Standards - Best Practice-至少具有有意义的名称至关重要。
在此处创建索引:http://msdn.microsoft.com/en-us/library/ms188783.aspx
关于c# - C#花费数小时的时间来执行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3609247/