Say I have these two objects:
OracleConnection connection = new OracleConnection(connectionString);
OracleCommand command = new OracleCommand(sql, connection);
To close the connection or Oracle, do I have to call command.Dispose(), connection.Dispose(), or both?
Is this good enough:
using(connection)
{
OracleDataReader reader = cmd.ExecuteReader();
// whatever...
}
Both answers are pretty much on target. You always want to call .Dispose() on any IDisposeable object. By wrapping in a "using" you tall the compiler to always impliment a try/finialy block for you.
1 point of note, if you want to avoid the nesting, you can write the same code like this:
using (OracleConnection connection = new OracleConnection(connectionString))
using (OracleCommand command = new OracleCommand(sql, connection))
using (OracleDataReader reader = cmd.ExecuteReader())
{
// do something here
}
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