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