简体   繁体   中英

How to call a stored procedure of oracle using C#?

Hi I have a stored procedure in oracle for insert in my table employee

Here it is my stored procedure it works fine

CREATE OR REPLACE PROCEDURE insert_Emp(idEmp in NUMBER, nameEmp in VARCHAR2, 
addressEmp in VARCHAR2, emailEmp in VARCHAR2,
phoneEmp in CHAR, sexoEmp in CHAR, duiEmp in CHAR, nitEmp in CHAR, salaryEmp in NUMBER, userEmp in VARCHAR2,
contraEmp in VARCHAR2, cargoEmp in VARCHAR2, activoEmp in CHAR)
IS
BEGIN
    INSERT INTO EMPLEADO(IdEmpleado, Nombre, Direccion, Email, Telefono, Sexo, Dui, Nit, Sueldo, Usuario, Contraseña, Cargo, Activo) 
    VALUES (idEmp, nameEmp, addressEmp, emailEmp, phoneEmp, sexoEmp, duiEmp, nitEmp, salaryEmp, userEmp, contraEmp, cargoEmp, activoEmp);
    COMMIT;
END;
/

But the problem is when I call it in my program from C#, I can't use my sequence EMP_SEQ.nextvalue

OracleCommand cmd = new OracleCommand("INSERT_EMP", DataBase.Conexion());
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("idEmp", OracleDbType.Decimal).Value = EMP_SEQ.nextval;
                    cmd.Parameters.Add("nameEmp", OracleDbType.Varchar2).Value = txtNombre.Text;
                    cmd.Parameters.Add("addressEmp", OracleDbType.Varchar2).Value = txtDireccion.Text;
                    cmd.Parameters.Add("emailEmp", OracleDbType.Varchar2).Value = txtEmail.Text;
                    cmd.Parameters.Add("phoneEmp", OracleDbType.Char).Value = txtTelefono.Text;
                    cmd.Parameters.Add("sexoEmp", OracleDbType.Char).Value = cb_sexo.Text;
                    cmd.Parameters.Add("duiEmp", OracleDbType.Char).Value = txtDUI.Text;
                    cmd.Parameters.Add("nitEmp", OracleDbType.Char).Value = txtNIT.Text;
                    cmd.Parameters.Add("salaryEmp", OracleDbType.Decimal).Value = txtSueldo.Text;
                    cmd.Parameters.Add("userEmp", OracleDbType.Varchar2).Value = txtUser.Text;
                    cmd.Parameters.Add("contraEmp", OracleDbType.Varchar2).Value = tb_contraseña.Text;
                    cmd.Parameters.Add("cargoEmp", OracleDbType.Varchar2).Value = cb_cargo.Text;
                    cmd.Parameters.Add("activoEmp", OracleDbType.Char).Value = obtenerEstado();
                    OracleDataAdapter da = new OracleDataAdapter(cmd);
                    cmd.ExecuteNonQuery();

So how can I put my sequence without any problem?

Shouldn't EMP_SEQ.nextvalue be used in the stored procedure, not sent to it? Something like:

INSERT INTO EMPLEADO(IdEmpleado, Nombre, Direccion, Email, Telefono, Sexo, Dui, Nit, Sueldo, Usuario, Contraseña, Cargo, Activo) 
VALUES (EMP_SEQ.nextvalue, nameEmp, addressEmp, emailEmp, phoneEmp, sexoEmp, duiEmp, nitEmp, salaryEmp, userEmp, contraEmp, cargoEmp, activoEmp);

(Or perhaps placed into a variable on a previous line and then use the variable in the query, whichever you prefer.)

Then your stored procedure wouldn't need to expect an identifier, it would be internally generating one. So the idEmp parameter can be removed entirely.

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