The situation is this, can anyone come across. Due to circumstances, I wrote a program for myself in Visual Studio 2008 on C # using OracleDataProvider for .NET. To do this, downloaded from the Oracle site ODAC112021Xcopy_32bit and copied a set of libraries into the project. The essence of this is how to handle the situation, if a connection is broken (say, a kill session) between the stages of opening a connection and a read request, i.e.

conn.Open(); //организуем запрос cmd.ExecuteReader() 

When checking such a case, the program I simply collapsed on ExecuteReader.

  • one
    And where is your conn.Open() ? - sp7
  • So it is necessary to check before executing the query, whether your connection is open. And in what state it is, and then suddenly you are there from another thread reading the data. If the stream is one, then it is necessary to check and restore the connection. but it's good to open and close each time after the connection is working. - Dmitry Gvozd
  • I’m writing just for this, if you break the connection, its status will remain Open, and there are no errors, just a collapse on the ExecuteReader line. - Inquiring
  • Well, well, I realized that, ideally, you need to open the connection, execute the request and close, and so on each time. But nevertheless, that if a session is killed in the moment between opening and execution, how then to be? - Inquiring
  • Does the program just "collapse" or does the usual exception crash there? - Pavel Mayorov

1 answer 1

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:

  1. Oracle Data Provider for .NET
  2. Try-catch-finally and try-finally blocks
  3. .NET Framework Data Provider for Oracle
  4. 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.

  • "Dispose () method" and you need to call it, what will change if you do not call it? thank. - 4per
  • one
    @ 3per, attached a couple of links in the answer, well, to be more specific: it frees unmanaged resources . If you don't use using , you must call the Dispose() method explicitly. - Denis Bubnov
  • Hmmm .... the problem is that I don’t have any mistakes at all, I knew about all of this and tried it already, I thought how else to steer. Libraries like copied everything they needed, but I'm not sure. If the request returns nothing or the session is killed, the program simply collapses on the ExecuteReader. - Questioner
  • @ Inquirer, I didn’t quite understand the meaning of it collapsing ... Exception is not a mistake. - Denis Bubnov
  • Everything, I apologize for the kipish, corrected the try..catch block and all the rules have become, apparently the thoughts are mixed up, what did, what did -_-. PS I understand correctly that it is impossible to keep the Open connection all the time, because is it counter productive? - Questioner