简体   繁体   中英

¿How to call existing stored procedure in oracle by EF?

I have a stored procedure that needs to be called from my project, I have run into this problem since I had never worked with Oracle and Entity Framework, could you help me in how to call the stored process ?.

This is my stored procedure:

create or replace procedure Random.SP_Random(r_cursor out sys_refcursor)
as
begin
    open r_Cursor for
        select element1, element2, element3 from Random.table;
end;

Here is sample Function to read Sys_RefCursor

    public void SPRandom()
    {
        OracleConnection connection = this.Database.GetOracleConnection();
        bool needClose = false;
        if (connection.State != ConnectionState.Open)
        {
            connection.Open();
            needClose = true;
        }

        try
        {
            using (OracleCommand cmd = connection.CreateCommand())
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = @"Random.SP_Random";

                OracleParameter outParameter = cmd.CreateParameter();
                outParameter.ParameterName = "r_cursor";
                outParameter.ParameterType = OracleDbType.Cursor;
                outParameter.Direction = ParameterDirection.Output;

                cmd.Parameters.Add(outParameter);


                using (OracleDataReader reader =  cmd.ExecuteReader())
                {
                    while(reader.Read())
                    {
                         .... read your data
                    }
                }
            }
        }
        finally
        {
            if (needClose)
            {
                connection.Close();
            }
        }
    }

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