I think you need to use the try-catch-finally or try-finally block . For example, it will look like this:
using (OracleConnection connection = new OracleConnection(connectionString)) { try { // Ваш код... conn.Open(); // Ваш код... cmd.ExecuteReader(): // Ваш код... } catch (Exception ex) { // либо обрабатываем исключение // либо ничего не делаем - можно вообще убрать блок 'catch' } finally { conn.Close(); // закрываем соединение } }
Useful links:
- Oracle Data Provider for .NET
- Try-catch-finally and try-finally blocks
- .NET Framework Data Provider for Oracle
- OracleCommand.ExecuteReader
It is better, of course, to put OracleConnection
into a using statement , because it is IDisposable , in order not to explicitly call the Dispose()
method, but you can forget about it in this case.
Example from MSDN site:
using (OracleConnection connection = new OracleConnection(connectionString)) { OracleCommand command = new OracleCommand(queryString, connection); connection.Open(); OracleDataReader reader = command.ExecuteReader(); try { while (reader.Read()) { Console.WriteLine(reader.GetValue(0)); } } finally { reader.Close(); } }
Using the finally block, you can clean up all the resources allocated in the try block, and you can run the code even when an exception occurs in the try block. As a rule, the statements in the finally block are executed when the control leaves the try statement.
conn.Open()
? - sp7