c# taking hours of time to execute
<p>Please find the code below it is taking hours of time to execute im comparing each string from database to calculate N00,N01,N10,N11 parameters please help its urgent. Temp1 is List of type string it consists of more than 5000 words </p>

<pre><code>foreach (string ri in temp1)
{
  for (int a3 = 0; a3 &lt; ssl.Count; a3++)
  {
    //for (int tn = 0; tn &lt; 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() + "&gt;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() + "&gt;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() + "&gt;0 and s.KeyWord='y' and s. " + ssl[a3].ToString() + "&gt;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);
      }
    }
  }
</code></pre>
