简体   繁体   中英

Returning data table from Oracle stored procedure that has input parameters to a .NET application

I have done some research into my issue, but I think I am struggling because of my lack of experience with Oracle (coming exclusively from a SQL Server background).

I'm trying to call an Oracle stored procedure from a .NET application and to return a data table to my application. I'm getting an "ORA-00900: invalid SQL statement" error.

Pseudocode-behind:

OracleConnection conn = new OracleConnection("...");
OracleCommand cmd = new OracleCommand("sproc_name", conn);
cmd.Connection = conn;
cmd.Parameters.Add("Year", OracleDbType.Int16).Value = vYear
cmd.Parameters.Add("Name", OracleDbType.Varchar2).Value = vName;
cmd.Parameters.Add("ID", OracleDbType.Varchar2).Value = vID;
conn.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());

Pseudo stored procedure:

CREATE OR REPLACE
PROCEDURE sproc_name
(
  Year IN NUMBER
, Name IN VARCHAR2
, ID IN VARCHAR2
)
AS
BEGIN
SELECT *
FROM TABLE
WHERE TABLE.Year = Year AND
      TABLE.Name = Name AND
      TABLE.ID = ID
END sproc_name;

I have read up on reference cursors and I tried to rewrite my procedure with no luck. How should I proceed here?

Try this

using (var conn = new OracleConnection("..."))

using (var cmd = conn.CreateCommand())
{

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sproc_name";
cmd.BindByName = true;

//...

Replying a bit old question, but its always good to reply an answered question, Oracle never give you output as sql. For getting result set you need to identify sys_refcursor as Out parameter.

So, your procedure will be looks like Also try to name proper (just a suggestion) :)

CREATE OR REPLACE PROCEDURE sproc_name (
  numYear IN NUMBER
, strName IN VARCHAR2
, numID IN VARCHAR2,
, cr_output out sys_refcursor
)
AS
BEGIN
open cr_output for
SELECT *
FROM TABLE
WHERE TABLE.Year = Year AND
      TABLE.Name = Name AND
      TABLE.ID = ID
END sproc_name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM