Possible Duplicate:
Is there any need to close a DbConnection if a using clause is used?
Does OledbConnection.Dispose()
close the connection?
I am aware that SqlConnection
does but what about OledbConnection
?
If the SqlConnection goes out of scope, it won't be closed. Therefore, you must explicitly close the connection by calling Close or Dispose . Close and Dispose are functionally equivalent. If the connection pooling value Pooling is set to true or yes , the underlying connection is returned back to the connection pool.
You should make use of the using block which will ensure the connection is closed and disposed correctly. using (OleDbConnection connection = new OleDbConnection(connectionString)) { connection. Open(); //Do some work }//The connection is automatically closed when the code exits the using block.
The OleDbConnection is opened and set as the Connection for the OleDbCommand. The example then calls ExecuteNonQuery and closes the connection. To accomplish this, ExecuteNonQuery is passed a connection string and a query string that is an SQL INSERT statement.
OleDbConnection("Provider = Microsoft. Jet. OLEDB. 4.0; Data Source = '\\csvFile.
Yes, it also does.
Source: OleDbConnection.Dispose Method (Boolean)
The Dispose method calls Close, and removes the OleDbConnection from the connection pool.
For detailed information see the Remarks
section on the reference link to know about the case of releasing both managed and unmanaged resources.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With