Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Same code running slower second time

Same code running slower second time

Scheduled Pinned Locked Moved Database
databasesql-serversysadmin
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    obelisk29
    wrote on last edited by
    #1

    Hello I am using Sql Server 2000 Developer Edition, Now I know there are limitations too how many connections you have BUT This piece of code below executes a whole lot slower the second time you run it.

    switch(cbSearch.SelectedItem.ToString())
    {
    	case "Lastname":
    		cmd = new SqlCommand("SPSearchRmiInfoLastname", conn);
    		cmd.CommandType = CommandType.StoredProcedure;
    	        cmd.Parameters.Add("@Lastname", txtSearch.Text);
    	        break;
    	case "Firstname":
    	        cmd = new SqlCommand("SPSearchRmiInfoFirstname", conn);
                    cmd.CommandType = CommandType.StoredProcedure;	
                    cmd.Parameters.Add("@Firstname", txtSearch.Text);
    	        break;
    }
    conn.Open();
    Search.Clear();
    
    SqlDataReader drd = cmd.ExecuteReader();
    while(drd.Read())
    {
    	Search.Rows.Add(new object\[\] {drd\[0\], drd\[1\], drd\[2\], drd\[3\]});
    	sbp.Text = Search.Rows.Count+" records found.";
    }
    dg.DataSource = Search;
    
    drd.Close();
    conn.Close();
    

    The results are staggering. First time: 1783 Milliseconds Second time: 150326 Milliseconds HOLY SHAZBOT!! Now that's a difference right there, Any ideas on why this is happening and how I can change it Thanks, Obe ------------------ I'm naked under my clothes...

    A 1 Reply Last reply
    0
    • O obelisk29

      Hello I am using Sql Server 2000 Developer Edition, Now I know there are limitations too how many connections you have BUT This piece of code below executes a whole lot slower the second time you run it.

      switch(cbSearch.SelectedItem.ToString())
      {
      	case "Lastname":
      		cmd = new SqlCommand("SPSearchRmiInfoLastname", conn);
      		cmd.CommandType = CommandType.StoredProcedure;
      	        cmd.Parameters.Add("@Lastname", txtSearch.Text);
      	        break;
      	case "Firstname":
      	        cmd = new SqlCommand("SPSearchRmiInfoFirstname", conn);
                      cmd.CommandType = CommandType.StoredProcedure;	
                      cmd.Parameters.Add("@Firstname", txtSearch.Text);
      	        break;
      }
      conn.Open();
      Search.Clear();
      
      SqlDataReader drd = cmd.ExecuteReader();
      while(drd.Read())
      {
      	Search.Rows.Add(new object\[\] {drd\[0\], drd\[1\], drd\[2\], drd\[3\]});
      	sbp.Text = Search.Rows.Count+" records found.";
      }
      dg.DataSource = Search;
      
      drd.Close();
      conn.Close();
      

      The results are staggering. First time: 1783 Milliseconds Second time: 150326 Milliseconds HOLY SHAZBOT!! Now that's a difference right there, Any ideas on why this is happening and how I can change it Thanks, Obe ------------------ I'm naked under my clothes...

      A Offline
      A Offline
      Aryo Handono
      wrote on last edited by
      #2

      Maybe you should change your store procedure, since you didn't post your SP, we don't know what kind of process happend there. "Courage choose who will follow, Fate choose who will lead" - Lord Gunner, Septerra Core "Press any key to continue, where's the ANY key ?" - Homer Simpsons Drinking gives me amazing powers of insight. I can solve all the worlds problems when drunk, but can never remember the solutions in the morning. - Michael P Butler to Paul Watson on 12/08/03

      O 1 Reply Last reply
      0
      • A Aryo Handono

        Maybe you should change your store procedure, since you didn't post your SP, we don't know what kind of process happend there. "Courage choose who will follow, Fate choose who will lead" - Lord Gunner, Septerra Core "Press any key to continue, where's the ANY key ?" - Homer Simpsons Drinking gives me amazing powers of insight. I can solve all the worlds problems when drunk, but can never remember the solutions in the morning. - Michael P Butler to Paul Watson on 12/08/03

        O Offline
        O Offline
        obelisk29
        wrote on last edited by
        #3

        This is my stored proc

        CREATE PROCEDURE dbo.SPSearchRmiInfoLastname
        (
        @Lastname varchar(50)
        )
        AS
        select * from RmiInfo where Lastname like @Lastname+'%' order by Lastname

        GO

        ------------------ I'm naked under my clothes...

        O 1 Reply Last reply
        0
        • O obelisk29

          This is my stored proc

          CREATE PROCEDURE dbo.SPSearchRmiInfoLastname
          (
          @Lastname varchar(50)
          )
          AS
          select * from RmiInfo where Lastname like @Lastname+'%' order by Lastname

          GO

          ------------------ I'm naked under my clothes...

          O Offline
          O Offline
          obelisk29
          wrote on last edited by
          #4

          WOW I figured it out I just put

          dg.DataSource = null;

          before it exeutes and it fixed it. somehow when you put a datatable into a datagrid whenever you add back to the datatable it is SLOW. hope this helps some people who come across this as well =) ------------------ I'm naked under my clothes...

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups