
Opening and Closing Connections
The two primary methods for connections are Open and Close. The Open method uses the information in the ConnectionString property to contact the data source and establish an open connection. The Close method shuts down the connection. Closing connections is essential, because most data sources support only a limited number of open connections, and open connections take up valuable system resources.
If you are working with TableAdapters, DataAdapters, or DataCommands, you do not have to explicitly open and close a connection. When you call a method of these objects (for example, an adapter's Fill or Update method), the method checks whether the connection is already open. If not, the adapter opens the connection, performs its logic, and closes the connection again.
Methods such as Fill only open and close the connection automatically if it is not already open. If the connection is open, then the methods use it but do not close it. This gives you the flexibility to open and close data commands yourself. You might do this if you have multiple adapters that share a connection. In that case, it is inefficient to have each adapter open and close the connection when you call its Fill method. Instead, you can open the connection, call the Fill method of each adapter, and then close the connection when you are done.