Using Close Dispose in ASP. Net

Source: Internet
Author: User

SqlConnection conn = new SqlConnection ("DataSource = LocalHost; Initial Catalog = DataBaseName; UserId = UserName; PassWard = 123456 ");
Conn. Open ();
Conn. Close ();
Conn. Dispose ();

Difference between Close and Dispose:
Close only closes the database link. You can Open it again after it is closed.
Dispose not only closes the database link, but also clears the connection. After the connection is cleared, it cannot be opened again. If you want to Open it again, you must assign a new value.
However, the two cannot destroy the SqlConnection object, that is, the resources occupied by the connection object in the database cannot be released. They will throw the SqlConnection object to the connection pool. if the object is not used for a certain period of time, it will be destroyed.

Dispose is automatically executed after the program is run in the Using statement block, and Dispose is still executed in the Using statement block even after the Return statement is jumped out, similar to using Finally in Try {} Catch {} Finally {}

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.