Search code examples
c#.netplsql

PL/SQL output using VC#


How can I get the output of my PL/SQL query into my code using the OracleDataReader

I have turned serveroutput on and tried to read the output as so But it always says no output

ConnectDB();
OracleCommand command1 = conn.CreateCommand();
command1.CommandText = "begin dbms_output.put_line('Output'); end;";
command1.CommandType = CommandType.Text;
OracleDataReader dr = command1.ExecuteReader();

if (dr.HasRows)
{
    while(dr.Read())
    {
    string str = dr[0].ToString();
    MessageBox.Show(str);
    }
}
else
    MessageBox.Show("No output");
conn.Close();

Solution

  • You can try perform a query, e.g.

    using OracleCommand command = conn.CreateCommand();
    
    // Typical Oracle feature - query Dual table to read a constant
    command.CommandText = 
      @"select 'Output'
          from Dual";
    
    using reader = command.ExecureReader();
    
    if (reader.Read()) 
        // Let me show general case, when we don't know field type
        MessageBox.Show(Convert.ToString(reader[0]));
    else
        MessageBox.Show("No output");