简体   繁体   中英

How can I get multiple output parameters from a stored procedure in a web service in C#?

I have a web service in C#, I use it to consults from tables, but I want to create a WebMethod to call a stored procedure and get back multiples output parameters. I can execute it with output parameters, it doesn't work when I try to call it whit outputs parameters.

This is a sample, I want to get back more that 2 parameters.

Stored procedure:

CREATE OR REPLACE PROCEDURE O_CAPEREZ.GIO_SP (
    VNOMBRE IN VARCHAR2,
    SALUDO  OUT VARCHAR2 )
AS
BEGIN
    INSERT INTO G_PRUEBA_SP(NOMBRE)
    VALUES (vNOMBRE);

    SALUDO:= ('Hello: ' || vNOMBRE);
END;

And this is my code in the web service, when I execute it using output variables I get this error

[HYC00] [Oracle][ODBC]Optional feature not implemented

C# code:

[WebMethod]
public string AP_Data(string curp)
{
     string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

     using (OdbcConnection con = new OdbcConnection(constr))
     {
            OdbcCommand cmd = new OdbcCommand("{CALL GIO_SP(?,?)}", con);

            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@vNOMBRE", (curp));
            cmd.Parameters.Add("@vNOMBRE", OdbcType.VarChar, 18);

            cmd.Connection.Open();
            cmd.ExecuteNonQuery();

            cmd.Parameters["@SALUDO"].Direction = ParameterDirection.ReturnValue;
            cmd.Connection.Close();

            string ret = Convert.ToString(cmd.Parameters["@SALUDO"].Value);
            return ret;   
    }
}

You have to add the parameter to the list even if you're not going to set a value there:

 cmd.Parameters.Add("@SALUDO", OdbcType.VarChar, 18).Direction = ParameterDirection.Output;

I don't know the the Oracle flavor is different, but in SQL I use ParameterDirection.ReturnValue rather than ParameterDirection.Output .

here's how i do it in MS SQL server 2008 But notice the data type and the lenth of the variables your create must be the same in your table

the stored proc create code

USE DATABASE DATABASE_NAME
GO
CREATE PROC SP_METHOD
@ID_CATIGORY INT,
@NAME VARCHAR (50),
@DESCRIPTION VARCHAR (50)   
AS
INSERT INTO TABLE_NAME
       ([ID_CAT]
       ,[NAME_PRODUCT]
       ,[DESC_PRODUCT]          
       )
 VALUES
       ( @ID_CATIGORY 
        ,@NAME 
        ,@DESCRIPTION )        
       GO

in the c# code

// Create SqlConnection 
       SqlConnection conn= new SqlConnection(@"Server=server_name; 
     DataBase=your_data_base_name;Integrated Security=false;User 
       Id=user_id;Password=password");

    // Open the Connection 
if (sqlconnection.State != ConnectionState.Open)
        {
            conn= .Open();
        }
// execute stored_procedure method don't change this 
public void ExecuteCommand(string stored_procedure, SqlParameter[] param)
    {
        SqlCommand sqlcomd = new SqlCommand();
        sqlcomd.CommandType = CommandType.StoredProcedure;
        sqlcomd.CommandText = stored_procedure;
        sqlcomd.Connection = sqlconnection;
        if (param !=null)
        {
            sqlcomd.Parameters.AddRange(param);
        }
        sqlcomd.ExecuteNonQuery();
    }
      // close connection method
      public void close_conn()
    {
        if (sqlconnection.State == ConnectionState.Open)
        {
            sqlconnection.Close();
        }
    }      
       // execute and retrieving data Method    
    public void Add_product(int ID_cat ,string Name_Product,string 
     Des_Product)
    {
       SqlParameter[] param = new SqlParameter[3];

        param[0] = new SqlParameter("@ID_CAT", SqlDbType.Int);
        param[0].Value = ID_cat;

        param[1] = new SqlParameter("@NAME_PRODUCT", SqlDbType.VarChar, 50);
        param[1].Value = Name_Product;

        param[2] = new SqlParameter("@DESC_PRODUCT", SqlDbType.VarChar, 50);
        param[2].Value = Des_Product;  

        ExecuteCommand("StoredProcedure_name", param);
        close_conn();
      }

and finally you can call this function

   Add_product(Convert.ToInt32(ComboBox.SelectedValue),txt_name.Text, 
   txt_desc.Text);

if there is any part you don't understand lemme know

I've seen many ways to accomplish this.

One way is to Pipe Delimit your select statement in your stored procedure and then use "Value1|Value2".Split('|')[0] to get Value1.

You could also return a table instead of using multiple parameters

DataTable table = new DataTable();
DataAdapter adapter = new DataAdapter(cmd);
adapter.fill(table);
return table.Rows[0]["Greeting"] + table.Rows[0]["Name"];

In the second example you can return as many 'Parameters' as you want, but you will have to assign them to their rightful spots later in your code.

I've also seen an XML way to do this same feature but I won't provide the code here since I don't personally think it is a very good way to do it. The way I've seen done was adding a bunch of XML attributes to a parent tag, and then coming back later and finding the value of each tag later in the code.

In MYSQL it would go like this

CREATE PROCEDURE O_CAPEREZ.GIO_SP (
    @vNOMBRE VARCHAR(50))
AS
BEGIN
    INSERT INTO G_PRUEBA_SP(NOMBRE)
    VALUES (@vNOMBRE);

    select 'Hola' as Greeting, @vNOMBRE as Name
END

Also note what Marc_s commented

You need to set the .Direction of the parameter BEFORE making the call to .ExecuteNonQuery()

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