C # should close the connection after accessing the database

Source: Internet
Author: User

1. Connection to the database SqlConnection con = new SqlConnection (constr); When you are finished, you should at least close or dispose of it. Failure to do so will result in an increase in the sleeping process in the database such as (SQl2005) and cannot be destroyed by itself, resulting in a "" Connection timeout that has reached the maximum number of connections.

The workaround: See Microsoft's official note "If the SqlConnection is out of range, it will not be closed." Therefore, unless you place the code inside a using statement, you must call Close or Dispose to explicitly close the connection. They are functionally equivalent. If you set the connection pool value Pooling to true or Yes, the physical connection is also released. ”
Https://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection.close (v=vs.80). aspx

Sqlconnection.clearpool (Con) can be added before 2.Close to clear each connection to prevent an already-connected database connection from entering sleeping and causing the user Connections of the number of connecting users to increase.

C # should close the connection after accessing the database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.