简体   繁体   中英

How to call stored procedure that returns 1 row?

How to call a procedure in C# to return 1 row only?

When I run this code I wrote, I get this error:

ORA-06502:PL/SQL: numeric or value error

Code:

con.Open();

OracleCommand comm = new OracleCommand();
comm.Connection = con;
comm.CommandText = "GetFirstName";
comm.CommandType = CommandType.StoredProcedure;

comm.Parameters.Add("ssnid", global.passuser);
comm.Parameters.Add("fname", OracleDbType.Varchar2).Direction = ParameterDirection.Output;

comm.ExecuteNonQuery();

textBox2.Text = comm.Parameters["fname"].ToString();

Procedure:

create or replace procedure  GetFirstName
(ssnid in VARCHAR2 ,  fname out VARCHAR2)
as
begin
select firstname
into fname
from passenger
where ssn = ssnid ;
end;

Try to use this syntax

cmd.Parameters.Add("ssnid",OracleDbType.Varchar2).Value = global.passuser;
 // or global.passuser.ToString();

and by the way fix this too

textBox2.Text = comm.Parameters["fname"].Value.ToString();

PROCEDURE

CREATE OR REPLACE PROCEDURE GETFIRSTNAME(ssnid VARCHAR2 ,  fname OUT VARCHAR2)

AS  
BEGIN

   SELECT 
   firstname INTO  fname
   FROM (SELECT 1 as ssn,'IM TOM CHANNEL' as firstname FROM dual)
   WHERE ssn = ssnid;

END GETFIRSTNAME;
```SQL

C# code is :

```C#
public void GETFIRSTNAME(string ssnid)
        {

            try
            {
                string vfname = "";
                ConnDB();
                OracleCommand cmd = new OracleCommand();
                cmd.Connection = con;
                cmd.CommandText = "GETFIRSTNAME";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("ssnid", OracleType.VarChar).Value = ssnid;
                cmd.Parameters.Add("fname", OracleType.VarChar,200).Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();
                vfname = cmd.Parameters["fname"].Value.ToString();
               
            }
            finally
            {
                con.Dispose();
                con.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