ktos1234 ktos1234 - 1 month ago 10
C# Question

Executing Oracle procedure

I connected my oracle database to visual studio and now I'm trying to execute procedure I created in my database.

I tried this:

OracleCommand cmd = new OracleCommand("BEGIN ADD_USER('"+txtName.Text+"','"+txtName2.Text+"',"+txtID.Text+"); END;" );

cmd.ExecuteNonQuery();


My procedure has 3 parameters : name, 2name, id. It works fine when I use this command in sqldeveloper, but I get error when I try it in my project.

Answer
using (OracleConnection cn = new OracleConnection("con string"))
{
    cn.Open();
    OracleCommand cmd = new OracleCommand("ADD_USER");
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Connection = cn;

    cmd.Parameters.AddWithValue(@YourSPParamName1, txtName.Text);
    cmd.Parameters.AddWithValue(@YourSPParamName2, txtName2.Text);
    cmd.Parameters.AddWithValue(@YourSPParamName3, txtID.Text);

    cmd.ExecuteNonQuery();
}

Something like this should work.