我在C#和SQL Server初学者,我写了这个查询创建SQL Server中的存储过程:我如何获得SQL Server存储过程在c#中的返回值?
create procedure newBehzad
@id bigint
as
DECLARE @ResultValue int
select *
from TABLEA
where id > @id
SET @ResultValue = -5
go
一切工作,和我写了这个C#代码来调用存储过程,它返回一个值:
using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand("newBehzad", conn)
{
CommandType = CommandType.StoredProcedure
})
{
conn.Open();
command.Parameters.Add("@id", SqlDbType.BigInt).Value = 2;
command.Parameters.Add("@ResultValue", SqlDbType.Int);
SqlParameter retval = command.Parameters.Add("@ResultValue", SqlDbType.Int);
retval.Direction = ParameterDirection.ReturnValue;
retunvalue = (string)command.Parameters["@ResultValue"].Value;
//SqlParameter retval = sqlcomm.Parameters.Add("@b", SqlDbType.VarChar);
command.ExecuteNonQuery();
conn.Close();
}
MessageBox.Show(returnValue);
但是当我运行的C#Windows应用程序,我得到这个错误:
Procedure or function newBehzad has too many arguments specified.
我该如何解决呢?谢谢。
ResultValue不是存储过程的参数。存储的产品是做什么的?它选择数据,但是您执行非查询。无义。 –
@GiorgiNakeuri谢谢你的回答,在我的TABELA上有一个id标识(1,1)字段,当选择那个TABLEA时,返回最新的id – user3671271