alecd4
alecd4

Reputation: 309

Trouble executing SQL stored procedure from C#

I have an existing stored procedure in SQL Server that I need to call from my C# code and get result. Here is how this SP looks like

CREATE PROCEDURE [dbo].[sp_MSnextID_DDB_NextID]
@tablesequence varchar(40)
AS
declare @next_id integer
begin transaction
update DDB_NextID
set DDB_SEQUENCE = DDB_SEQUENCE + 1
where DDB_TABLE = @tablesequence
select @next_id = DDB_SEQUENCE from DDB_NextID 
where DDB_TABLE = @tablesequence
commit transaction
RETURN @next_id

Here is my C# code

using (OdbcConnection connection = new OdbcConnection(GetConnectionString()))
{
   using (IDbCommand command = new OdbcCommand())
   {
      command.CommandText = "sp_MSnextID_DDB_NEXTID";
      command.CommandType = CommandType.StoredProcedure;
      IDbDataParameter parameter1 = command.CreateParameter();
      parameter1.DbType = DbType.String;
      parameter1.ParameterName = "@tablesequence";
      parameter1.Value = "ddb_dc_document";
      parameter1.Direction = ParameterDirection.Input;
      command.Parameters.Add(parameter1);

      IDbDataParameter parameter2 = command.CreateParameter();
      parameter2.DbType = DbType.Int32;
      parameter2.ParameterName = "@Return_Value";
      parameter2.Direction = ParameterDirection.ReturnValue;
      command.Parameters.Add(parameter2);
      command.Connection = connection;
      connection.Open();
      command.ExecuteNonQuery();
      IDbDataParameter o = (command.Parameters)["@Return_Value"] as IDbDataParameter;
      //Got return value from SP in o.Value
   }
}

The trouble is I am getting exception

[42000] [Microsoft][SQL Native Client][SQL Server]Procedure or function 'sp_MSnextID_DDB_NextID' expects parameter '@tablesequence', which was not supplied.

which I can't explain or fix. What I am missing?

To find a way around, I was trying different approach: executing the following query that sets data in a temp table

create table #temp  (i integer); insert into #temp exec sp_MSNextID_DDB_NEXTID @tablesequence='ddb_dc_document';select * from #temp;

In this case SP is executed correctly but select returns zero rows!

Upvotes: 1

Views: 1568

Answers (2)

moribvndvs
moribvndvs

Reputation: 42497

Unfortunately, you can't use named parameters with OdbcCommand. You will need to instead execute a call statement to your stored procedure.

using (OdbcConnection connection = new OdbcConnection(GetConnectionString()))
{
   using (IDbCommand command = new OdbcCommand())
   {
      command.CommandText = "{ ? = CALL sp_MSnextID_DDB_NEXTID(?) }";
      command.CommandType = CommandType.StoredProcedure;


      IDbDataParameter parameter2 = command.CreateParameter();
      parameter2.DbType = DbType.Int32;
      parameter2.ParameterName = "@Return_Value";
      parameter2.Direction = ParameterDirection.ReturnValue;
      command.Parameters.Add(parameter2);

      IDbDataParameter parameter1 = command.CreateParameter();
      parameter1.DbType = DbType.String;
      parameter1.ParameterName = "@tablesequence";
      parameter1.Value = "ddb_dc_document";
      parameter1.Direction = ParameterDirection.Input;
      command.Parameters.Add(parameter1);

      command.Connection = connection;
      connection.Open();
      command.ExecuteNonQuery();
      IDbDataParameter o = (command.Parameters)["@Return_Value"] as IDbDataParameter;
      //Got return value from SP in o.Value
   }
}

Upvotes: 1

Andrey Gurinov
Andrey Gurinov

Reputation: 2885

To make your workaround work you should replace

RETURN @next_id 

in your procedure with

SELECT @next_id 

Upvotes: 0

Related Questions