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. General Programming
  3. C#
  4. Slow Query on 2nd query of the same command

Slow Query on 2nd query of the same command

Scheduled Pinned Locked Moved C#
databasehelpperformanceannouncement
4 Posts 3 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.
  • A Offline
    A Offline
    AesopTurtle
    wrote on last edited by
    #1

    I've created a class containing a method to make a connection with a database and do SELECT, INSERT, UPDATE, DELETE. The class works just fine... only for the first query. The problem occurs when the second query is made. The result of the second query is just fine but at VERY VERY slow rate (almost not responding). Here's my class:

    class MyClass
    {
    public string SQL2KConnStr = "..(my connection string)..";
    public object ExecuteSql(string sql)
    {
    SqlConnection conn = new SqlConnection(this.SQLExpressConnStr);
    if (conn.State == ConnectionState.Open)
    {
    conn.Close();
    }
    conn.Open();
    switch(sql.Trim().Substring(0,6).ToLower()){
    case "select":
    SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
    DataSet ds = new DataSet();
    adapter.Fill(ds, "QueriedTab");
    return ds.Tables["QueriedTab"]; //Return a data table.
    break;
    default:
    SqlCommand cmd = new SqlCommand();
    //cmd.CommandType = CommandType.Text;
    cmd.Connection = conn;
    cmd.CommandText = sql;
    return cmd.ExecuteNonQuery(); //Return a number of affected rows.
    }
    }

    And this's another class that makes use of the class above:

    string SqlSelect = "select * from ...";
    MyClass mc = new MyClass();
    DataTable tab = (DataTable)mc.ExecuteSql(SqlSelect);
    myDataGrid.DataSource = tab;
    tab.Dispose();

    I display the resultin a datagrid. The correctness of the result is fine. But the speed of the second and after is SO SO slow even though i've disposed the returned table every time. Anybody help me please... KiT

    L R 2 Replies Last reply
    0
    • A AesopTurtle

      I've created a class containing a method to make a connection with a database and do SELECT, INSERT, UPDATE, DELETE. The class works just fine... only for the first query. The problem occurs when the second query is made. The result of the second query is just fine but at VERY VERY slow rate (almost not responding). Here's my class:

      class MyClass
      {
      public string SQL2KConnStr = "..(my connection string)..";
      public object ExecuteSql(string sql)
      {
      SqlConnection conn = new SqlConnection(this.SQLExpressConnStr);
      if (conn.State == ConnectionState.Open)
      {
      conn.Close();
      }
      conn.Open();
      switch(sql.Trim().Substring(0,6).ToLower()){
      case "select":
      SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
      DataSet ds = new DataSet();
      adapter.Fill(ds, "QueriedTab");
      return ds.Tables["QueriedTab"]; //Return a data table.
      break;
      default:
      SqlCommand cmd = new SqlCommand();
      //cmd.CommandType = CommandType.Text;
      cmd.Connection = conn;
      cmd.CommandText = sql;
      return cmd.ExecuteNonQuery(); //Return a number of affected rows.
      }
      }

      And this's another class that makes use of the class above:

      string SqlSelect = "select * from ...";
      MyClass mc = new MyClass();
      DataTable tab = (DataTable)mc.ExecuteSql(SqlSelect);
      myDataGrid.DataSource = tab;
      tab.Dispose();

      I display the resultin a datagrid. The correctness of the result is fine. But the speed of the second and after is SO SO slow even though i've disposed the returned table every time. Anybody help me please... KiT

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      KiTsuNeKo wrote:

      if (conn.State == ConnectionState.Open) { conn.Close(); }

      Rather close the connection after your query. xacc-ide 0.0.99-preview7 now with C#, C, C++, IL, XML, Nemerle, IronPython, Perl, Caml, SML, Ruby, Flex, Yacc, Java, Javascript, Lua, Prolog and Boo highlighting support!

      1 Reply Last reply
      0
      • A AesopTurtle

        I've created a class containing a method to make a connection with a database and do SELECT, INSERT, UPDATE, DELETE. The class works just fine... only for the first query. The problem occurs when the second query is made. The result of the second query is just fine but at VERY VERY slow rate (almost not responding). Here's my class:

        class MyClass
        {
        public string SQL2KConnStr = "..(my connection string)..";
        public object ExecuteSql(string sql)
        {
        SqlConnection conn = new SqlConnection(this.SQLExpressConnStr);
        if (conn.State == ConnectionState.Open)
        {
        conn.Close();
        }
        conn.Open();
        switch(sql.Trim().Substring(0,6).ToLower()){
        case "select":
        SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        adapter.Fill(ds, "QueriedTab");
        return ds.Tables["QueriedTab"]; //Return a data table.
        break;
        default:
        SqlCommand cmd = new SqlCommand();
        //cmd.CommandType = CommandType.Text;
        cmd.Connection = conn;
        cmd.CommandText = sql;
        return cmd.ExecuteNonQuery(); //Return a number of affected rows.
        }
        }

        And this's another class that makes use of the class above:

        string SqlSelect = "select * from ...";
        MyClass mc = new MyClass();
        DataTable tab = (DataTable)mc.ExecuteSql(SqlSelect);
        myDataGrid.DataSource = tab;
        tab.Dispose();

        I display the resultin a datagrid. The correctness of the result is fine. But the speed of the second and after is SO SO slow even though i've disposed the returned table every time. Anybody help me please... KiT

        R Offline
        R Offline
        Rob Graham
        wrote on last edited by
        #3

        You are creating a new connection object each call to the function, and you're not closing it when done (garbage collection will eventually do so, but this is not good practice. try this:

        public class MyClass
        {
        	private SqlConnection conn = new SqlConnection(this.SQLExpressConnStr);
        	public string SQL2KConnStr = "..(my connection string)..";
        	public object ExecuteSql(string sql)
        	{
        		
        		if (conn.State != ConnectionState.Open)
        		{
        			conn.Open();
        		}
        		switch(sql.Trim().Substring(0,6).ToLower())
        		{
        			case "select":  
        				try
        				{
        					SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
        					ds = new DataSet();
                                                    adapter.Fill(ds, "QueriedTab");
        				}
        				finally
        				{
        					conn.Close();
        				}
        				return ds.Tables\["QueriedTab"\]; //Return a data table.
        				break;   
        			default:      
        				SqlCommand cmd = new SqlCommand();      
        				//cmd.CommandType = CommandType.Text;      
        				cmd.Connection = conn;      
        				cmd.CommandText = sql; 
        				long rtn = 0;
        				try
        				{
        					rtn = cmd.ExecuteNonQuery(); //Return a number of affected rows.
        				}
        				finally
        				{
        					conn.Close();
        				}
        				return rtn;
        				break;
        		}																																														   
        	}
        }
        

        Only create the connection once, open and close it with each use. You might want to add catch(SqlException e) blocks before each finally block to handle errors. probably want to move the declaration of the dataset to class or function scope (out of the try block) Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke

        A 1 Reply Last reply
        0
        • R Rob Graham

          You are creating a new connection object each call to the function, and you're not closing it when done (garbage collection will eventually do so, but this is not good practice. try this:

          public class MyClass
          {
          	private SqlConnection conn = new SqlConnection(this.SQLExpressConnStr);
          	public string SQL2KConnStr = "..(my connection string)..";
          	public object ExecuteSql(string sql)
          	{
          		
          		if (conn.State != ConnectionState.Open)
          		{
          			conn.Open();
          		}
          		switch(sql.Trim().Substring(0,6).ToLower())
          		{
          			case "select":  
          				try
          				{
          					SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
          					ds = new DataSet();
                                                      adapter.Fill(ds, "QueriedTab");
          				}
          				finally
          				{
          					conn.Close();
          				}
          				return ds.Tables\["QueriedTab"\]; //Return a data table.
          				break;   
          			default:      
          				SqlCommand cmd = new SqlCommand();      
          				//cmd.CommandType = CommandType.Text;      
          				cmd.Connection = conn;      
          				cmd.CommandText = sql; 
          				long rtn = 0;
          				try
          				{
          					rtn = cmd.ExecuteNonQuery(); //Return a number of affected rows.
          				}
          				finally
          				{
          					conn.Close();
          				}
          				return rtn;
          				break;
          		}																																														   
          	}
          }
          

          Only create the connection once, open and close it with each use. You might want to add catch(SqlException e) blocks before each finally block to handle errors. probably want to move the declaration of the dataset to class or function scope (out of the try block) Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke

          A Offline
          A Offline
          AesopTurtle
          wrote on last edited by
          #4

          I've tried the solution but the problem still exists... T-T There're only 130 records in the database and it shouldn't take so long to query them all. KiT

          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