tags:

views:

43

answers:

3

Is this the proper way to clean up after myself when using a connection to an oracle-server, using system.data.oracleclient?

using System.Data.OracleClient;

var con = new OracleConnection("some connection string");
con.Open();
con.Close();
con = null;
A: 

This may be useful for you.

Ngu Soon Hui
+1  A: 

Yes it is.
You might also want to call con.Dispose() after Close. This makes sure the connection is released immediately without waiting for the .NET garbage collector.

Remember to also Close any readers you use after reading data.

awe
Actually, Dispose() calls Close() for you (from looking at it in reflector). So you only need to call Dispose(), or wrap the OracleConnection in a using {...} block
thecoop
+3  A: 

OracleConnection implements IDisposable so a good idea is to employ the using construct. I.e.

using(var connection = new OracleConnection("connection string")) {
   // do stuff
}

This will make sure the connection is closed and disposed even in the case of an error. I don't see any reason to null the reference. It will go out of scope and will be cleaned so no need to clutter the code with that IMO.

Brian Rasmussen