2

I'm trying to use stored function from Oracle Express database in c# application.

OdbcCommand com = new OdbcCommand("SILNIA",sqlConn);
com.CommandType = CommandType.StoredProcedure;

OdbcParameter sqlParam = new OdbcParameter("@ReturnValue", OdbcType.Int);
sqlParam.Direction = ParameterDirection.ReturnValue;
com.Parameters.Add(sqlParam);
com.ExecuteNonQuery();

label1.Content = com.Parameters["@ReturnValue"].Value.ToString();

where SILNIA function (compiles without errors) is defined:

create or replace
FUNCTION SILNIA RETURN NUMBER IS
w NUMBER := 1;
BEGIN
for i in 1..5 loop
w:=w*i;
end loop;

RETURN w;
END SILNIA;

On line:

com.ExecuteNonQuery();

i get

ERROR [42000] [Microsoft][ODBC driver for Oracle][Oracle]ORA-00900: invalid SQL statement.

But this works works without any problems:

OdbcCommand sqlCom = new OdbcCommand("SELECT SILNIA() FROM DUAL", sqlConn);
label1.Content = sqlCom.ExecuteScalar().ToString();
mmatloka
  • 1,986
  • 1
  • 20
  • 46

2 Answers2

1

You can't use ExecuteNonQuery() to run a SP. You have to do something else -- ExecuteScalar or ExecuteReader

This is because ExecuteNonQuery() is designed to only work with UPDATE, INSERT, and DELETE statements.

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • I've tried both OdbcDataReader r = com.ExecuteReader(); and label1.Content = (string)com.ExecuteScalar(); instead of com.ExecuteNonQuery(); and the same error occurs. – mmatloka Nov 13 '10 at 22:21
  • 1
    You sure you can't run ExecuteNonQuery() for SP's? I'm fairly confident you can call it... – mint Nov 16 '10 at 20:10
0

Your function accepts no parameters - try this:

SELECT SILNIA FROM DUAL
Jeffrey Kemp
  • 59,135
  • 14
  • 106
  • 158