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 {}