How to call a "Stored Procedure" oracle c#

Asked

Viewed 2,196 times

1

I have the following Procedure in my bank:

inserir a descrição da imagem aqui

I’m trying to call my store Procedure in the .net in this way:

        public int CountEvent(int resourceId, int eventCounterDefinitionId, DateTime startDate, DateTime stopDate)
    {         
        int ret = 0;
        using (OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings[MyConnectionStrings.Default].ConnectionString))
        {
            try
            {
                conn.Open();
                Console.WriteLine("ServerVersion: {0} \nDataSource: {1} \nHostName: {2}",
                    conn.ServerVersion, conn.DataSource, conn.HostName);

                OracleCommand com = new OracleCommand("PRODUCTION.COUNT_EVENT", conn);
                OracleParameter paramiter1 = new OracleParameter();
                com.Parameters.Add("@P_RESOURCEID", resourceId);
                com.Parameters.Add("@P_EVENTCOUNTERDEFINITIONID",eventCounterDefinitionId);
                com.Parameters.Add("@P_SHIFT_START", startDate);
                com.Parameters.Add("@P_SHIFT_END", stopDate);
                com.CommandType = System.Data.CommandType.StoredProcedure;
                com.ExecuteReader();

            }
            catch (Exception ex)
            {                    
                Console.WriteLine(ex.Message);
            }
        }            
        return ret;
    }

However, when executing my code, it gives the error message:

Incorrect number of argument types in the call to 'COUNT_EVENT'

Could someone help me in why the parameters are wrong?

  • Next time try not to post images and without the code snippets.

1 answer

1


Your trial has an output variable (out), which must be declared in your c# call as well.

Consuming an output variable

ora_cmd.Parameters.Add("Lc_Exito", OracleDbType.Int32).Direction = ParameterDirection.Output;

ora_cmd.ExecuteNonQuery();

var returno = ora_cmd.Parameters["Lc_Exito"].value

So your code should work.

Cursor

I also noticed that the out of your query is an oracle cursor, a more complicated type of work. The Code to consume it would be something like this:

cmd.Parameters.Add("REC_CUR",OracleType.Cursor).Direction=ParameterDirection.Output;
con.Open();  
cmd.ExecuteNonQuery();

And you’ll catch him like this:

cmd.Parameters["REC_CUR"].value
  • I tried to adapt the code, but it still didn’t work.

  • 2

    Same mistake? posted as the code changed

  • posted the code in the replies, added only the output variable

  • 2

    I edited minah reply, I noticed that you are using a courses out of the store

  • 1

    Thanks so much for the help and the time dedicated, it worked, the problem really was the output parameter!!

  • 2

    BOAAAAAA!!! TAMO TOGETHER! :)

  • And Lembresse that all parameters have to be called, and the type courses are more difficult to deal with. Good morning, good job.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.