Open a SQL Connection, do work, and close the connection [on hold]











up vote
0
down vote

favorite












Am I closing my SQL connection correctly, by placing it inside a "using" block?



This is how I grab a new connection, execute a query, and return the results:



using (SqlConnection objCS = DB.LMSAdminConn())
{
objCS.Open();
SqlCommand objCommand = new SqlCommand("SELECT TOP 1 * FROM users WHERE userid = @userid", objCS);
objCommand.Parameters.Add("@revisionid", SqlDbType.Int).Value = userid;
SqlDataReader reader = objCommand.ExecuteReader();
while (reader.Read())
{
//do something
}
reader.Close();
}


The connection itself comes from this call:



public static SqlConnection LMSAdminConn()
{
return new SqlConnection(ConfigurationManager.ConnectionStrings["lmsadmin"].ToString());
}


I am opening the connection inside a "using" block, and I thought that the connection would be closed as well, because it is opened inside the "using" block. But since the "new SqlConnection" object is actually generated from an outside class, is my connection still getting appropriately closed? SQL Server shows the connection as still being open, but I'm not sure if that is ADO.NET connection pool recycling / sharing, or if the connection is truly being held open.










share|improve this question







New contributor




Tim Westover is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by Toby Speight, Graipher, Jesse C. Slicer, vnp, Mast yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Graipher, vnp, Mast

If this question can be reworded to fit the rules in the help center, please edit the question.

















    up vote
    0
    down vote

    favorite












    Am I closing my SQL connection correctly, by placing it inside a "using" block?



    This is how I grab a new connection, execute a query, and return the results:



    using (SqlConnection objCS = DB.LMSAdminConn())
    {
    objCS.Open();
    SqlCommand objCommand = new SqlCommand("SELECT TOP 1 * FROM users WHERE userid = @userid", objCS);
    objCommand.Parameters.Add("@revisionid", SqlDbType.Int).Value = userid;
    SqlDataReader reader = objCommand.ExecuteReader();
    while (reader.Read())
    {
    //do something
    }
    reader.Close();
    }


    The connection itself comes from this call:



    public static SqlConnection LMSAdminConn()
    {
    return new SqlConnection(ConfigurationManager.ConnectionStrings["lmsadmin"].ToString());
    }


    I am opening the connection inside a "using" block, and I thought that the connection would be closed as well, because it is opened inside the "using" block. But since the "new SqlConnection" object is actually generated from an outside class, is my connection still getting appropriately closed? SQL Server shows the connection as still being open, but I'm not sure if that is ADO.NET connection pool recycling / sharing, or if the connection is truly being held open.










    share|improve this question







    New contributor




    Tim Westover is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.











    put on hold as off-topic by Toby Speight, Graipher, Jesse C. Slicer, vnp, Mast yesterday


    This question appears to be off-topic. The users who voted to close gave this specific reason:


    • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Graipher, vnp, Mast

    If this question can be reworded to fit the rules in the help center, please edit the question.















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Am I closing my SQL connection correctly, by placing it inside a "using" block?



      This is how I grab a new connection, execute a query, and return the results:



      using (SqlConnection objCS = DB.LMSAdminConn())
      {
      objCS.Open();
      SqlCommand objCommand = new SqlCommand("SELECT TOP 1 * FROM users WHERE userid = @userid", objCS);
      objCommand.Parameters.Add("@revisionid", SqlDbType.Int).Value = userid;
      SqlDataReader reader = objCommand.ExecuteReader();
      while (reader.Read())
      {
      //do something
      }
      reader.Close();
      }


      The connection itself comes from this call:



      public static SqlConnection LMSAdminConn()
      {
      return new SqlConnection(ConfigurationManager.ConnectionStrings["lmsadmin"].ToString());
      }


      I am opening the connection inside a "using" block, and I thought that the connection would be closed as well, because it is opened inside the "using" block. But since the "new SqlConnection" object is actually generated from an outside class, is my connection still getting appropriately closed? SQL Server shows the connection as still being open, but I'm not sure if that is ADO.NET connection pool recycling / sharing, or if the connection is truly being held open.










      share|improve this question







      New contributor




      Tim Westover is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Am I closing my SQL connection correctly, by placing it inside a "using" block?



      This is how I grab a new connection, execute a query, and return the results:



      using (SqlConnection objCS = DB.LMSAdminConn())
      {
      objCS.Open();
      SqlCommand objCommand = new SqlCommand("SELECT TOP 1 * FROM users WHERE userid = @userid", objCS);
      objCommand.Parameters.Add("@revisionid", SqlDbType.Int).Value = userid;
      SqlDataReader reader = objCommand.ExecuteReader();
      while (reader.Read())
      {
      //do something
      }
      reader.Close();
      }


      The connection itself comes from this call:



      public static SqlConnection LMSAdminConn()
      {
      return new SqlConnection(ConfigurationManager.ConnectionStrings["lmsadmin"].ToString());
      }


      I am opening the connection inside a "using" block, and I thought that the connection would be closed as well, because it is opened inside the "using" block. But since the "new SqlConnection" object is actually generated from an outside class, is my connection still getting appropriately closed? SQL Server shows the connection as still being open, but I'm not sure if that is ADO.NET connection pool recycling / sharing, or if the connection is truly being held open.







      sql .net sql-server asp.net






      share|improve this question







      New contributor




      Tim Westover is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Tim Westover is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Tim Westover is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      Tim Westover

      1




      1




      New contributor




      Tim Westover is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Tim Westover is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Tim Westover is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      put on hold as off-topic by Toby Speight, Graipher, Jesse C. Slicer, vnp, Mast yesterday


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Graipher, vnp, Mast

      If this question can be reworded to fit the rules in the help center, please edit the question.




      put on hold as off-topic by Toby Speight, Graipher, Jesse C. Slicer, vnp, Mast yesterday


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Graipher, vnp, Mast

      If this question can be reworded to fit the rules in the help center, please edit the question.



























          active

          oldest

          votes






















          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes

          Popular posts from this blog

          Morgemoulin

          Scott Moir

          Souastre