Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
How can I write this code more efficiently so it will execute faster?

C#
DataTable dt1 = new DataTable();
dt1.Load(DbInfo.DataRdr(Conn, "SELECT DISTINCT     Events.Location FROM Events WHERE Events.Location NOT IN (SELECT Code FROM Locations)"));
  int cnt = 0;
  for (int i = 0; i < dt1.Rows.Count; i++)
  {
    Logging.Write(SetLength(" ", 10) + dt1.Rows[i]["Location"].ToString());
  }




---------------------
DbInfo.DataRdr returns a SqlDataReader object.
Posted
Comments
wizardzz 13-Apr-11 17:03pm    
What's taking long, the query or the loop?

This should run a little faster
C#
SqlDataReader reader = DbInfo.DataRdr(Conn, "SELECT DISTINCT Events.Location FROM Events WHERE 
NOT EXISTS( SELECT 1 FROM Location WHERE Code = Events.Location")
int colOrdinal = reader.GetOrdinal("Location");
while(reader.Read())
   {
   Logging.Write(SetLength(" ", 10) + reader.GetString(colOrdinal));
   }
 
Share this answer
 
Comments
Mastersev 13-Apr-11 23:01pm    
thank you very much sir
Costica U 14-Apr-11 11:06am    
You are welcome
If you write 5,000,000 records, yes it will be very slow. Read this, I hope it helps you.

left-outer-join-vs-not-exists1/[^]

left-outer-join-vs-not-exists2/[^]
 
Share this answer
 
In my openion above code is good.
 
Share this answer
 

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