1

我在 SQL Server 中有一个现有的存储过程,我需要从我的 C# 代码中调用它并获取结果。这是这个 SP 的样子

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

这是我的 C# 代码

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
   }
}

问题是我遇到了异常

[42000] [Microsoft][SQL Native Client][SQL Server]过程或函数“sp_MSnextID_DDB_NextID”需要参数“@tablesequence”,但未提供该参数。

我无法解释或修复。我错过了什么?

为了找到解决方法,我尝试了不同的方法:执行以下查询,在临时表中设置数据

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

在这种情况下 SP 正确执行,但 select 返回零行!

4

2 回答 2

1

不幸的是,您不能将命名参数与OdbcCommand. 您将需要改为对存储过程执行调用语句。

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
   }
}
于 2012-04-08T05:58:36.743 回答
0

为了使您的解决方法起作用,您应该更换

RETURN @next_id 

在您的程序中

SELECT @next_id 
于 2012-04-08T05:57:37.353 回答