3

The problem is as simple as stated in question topic. I try to run some query using INSERT...RETURNING clause, which raises ORA-12537 exception when I try to execute it. The source is as follows:

using ( OracleCommand command = new OracleCommand () ) {
    command.Connection = connection;
    command.BindByName = true;
    command.CommandText = "INSERT INTO objects(name)VALUES(:objectName)RETURNING id INTO :objectId";
    command.Parameters.Add ( "objectName", OracleDbType.Varchar2, ParameterDirection.Input );
    command.Parameters.Add ( "objectId", OracleDbType.Int64, ParameterDirection.Output );
    command.ExecuteNonQuery ();
}

Execution of the last line leads to OracleException being raised with message ORA-12537: Network Session: End of file. Running same query without RETURNING sub-clause goes obviously smooth.

2 Answers2

4

OracleCommand.CommandText property sets the SQL statement or stored procedure to execute.
The ORA-12537 is an information message only and means that the connection has been closed. This can be caused by a number of reasons, i.a. oracle can't execute the sql statement properly and terminates the session.

Try to execute the statement as pl/sql block instead of in a pure sql context:

command.CommandText = @"
    begin 
        insert into objects(name) values(:objectName) returning id into :objectId; 
    end;";
0xdb
  • 3,539
  • 1
  • 21
  • 37
  • It'd be nice to add that ODP.NET still allows to do just so, i.e. write something like `"BEGIN\nINSERT INTO objects(name)VALUES('somename')RETURNING id INTO :objectId;\nEND;"` and get returned value back from an output parameter. – Aleksei Omelaienko Jan 24 '17 at 22:16
2

I had the same problem too and initially used @0xdb solution and it worked. Then, I found that the ParameterDirection should be ReturnValue and not Output and everything worked successfully.

using ( OracleCommand command = new OracleCommand () ) {
    command.Connection = connection;
    command.BindByName = true;
    command.CommandText = "INSERT INTO objects(name)VALUES(:objectName)RETURNING id INTO :objectId";
    command.Parameters.Add ( "objectName", OracleDbType.Varchar2, ParameterDirection.Input );
    command.Parameters.Add ( "objectId", OracleDbType.Int64, ParameterDirection.ReturnValue);
    command.ExecuteNonQuery ();
}
Pedro Silva
  • 2,655
  • 1
  • 15
  • 24