About using Keywords

Source: Internet
Author: User

Accessing database resources using C # requires the following steps:

SqlConnection con=New  SqlConnection (str); Try {    con. Open ();     // slightly }catch(Exception ex) {    throw  ex;} finally {    con. Close ();}

Databases typically belong to a limited resource manager, so you should close them immediately after use

This way of writing ensures that even if an exception occurs, the connection will be released.

In addition to using the try-catch-finally statement, you can use the using statement to free resources

Two functions of using:

1. Import namespaces

2. Releasing the memory resources occupied by the object

Grammar:

using (SqlConnection con=new  SqlConnection (str)) {  // operation code }

When the program executes at the end of a using statement, which is going to be out of {}, the CLR immediately releases the object by calling the object's Dispose () method

Dispose () Method:

Can those objects use the using recycle?

The type of the IDisposable interface must be implemented to use the using recycle

are managed or unmanaged resources recycled by using?

Recovering unmanaged Resources

About using Keywords

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.