简体   繁体   English

“过程或函数需要未提供的参数”错误

[英]'Procedure or function expects parameter, which was not supplied' error

I have a method in a service that is calling a stored procedure in a SQL Server database and I'm getting an error: 我在服务中有一个方法正在调用SQL Server数据库中的存储过程,但出现错误:

Procedure or function 'GenerateInviteKey' expects parameter '@inviterId', which was not supplied 过程或函数“ GenerateInviteKey”需要未提供的参数“ @inviterId”

My stored procedure works when I execute it in SQL Server Mgmt Studio, so I'm pretty sure its my C# method that's causing the error. 当我在SQL Server Mgmt Studio中执行存储过程时,该存储过程可以工作,因此,我很确定它是导致错误的C#方法。 My code looks like this: 我的代码如下所示:

public class Invite
{
        public Int64 InviterId { get; set; }
        public string InviterName { get; set; }
        public string InviteeEmail { get; set; }
}

private string CreateInviteKey(Invite invite)
{
    var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);

    try
    {
        conn.Open();

        var command = new SqlCommand("GenerateInviteKey", conn);
        command.Parameters.Add(new SqlParameter("@inviterId", SqlDbType.BigInt, 0, "inviter_id"));
        command.Parameters.Add(new SqlParameter("@inviteeEmail", SqlDbType.VarChar, 100, "invitee_email"));
        command.Parameters.Add(new SqlParameter("@inviteKey", SqlDbType.VarChar, 30, "invite_key"));

        command.Parameters[0].Value = invite.InviterId;
        command.Parameters[1].Value = invite.InviteeEmail;
        command.Parameters[2].Direction = ParameterDirection.Output;

        command.ExecuteNonQuery();

        var inviteKey = (string)command.Parameters[2].Value;

        return inviteKey;
   }
   catch (Exception ex)
   {
       var error = ex.Message;
       return null;
   }
   finally
   {
       conn.Close();
       conn.Dispose();
   }
}

And the stored procedure looks like this: 存储过程如下所示:

ALTER PROCEDURE [dbo].[GenerateInviteKey] 
    @inviterId int,
    @inviteeEmail varchar(100), 
    @inviteKey varchar(30) OUT
AS
BEGIN
    SET NOCOUNT ON;

    exec dbo.GenerateRandomString 1, 1, 1, null, 30, @inviteKey OUT

    INSERT INTO [dbo].[invite_key] ([invite_key], [inviter_id], [invitee_email], [eff_datetime])
       SELECT 
          @inviteKey, @inviterId, @inviteeEmail, GETDATE()
END

I've been staring at this thing for an hour now trying to find what I've messed up, but I need another set of eyes. 我一直在盯着这个东西一个小时,试图找到我搞砸的东西,但是我需要另一双眼睛。

UPDATE 1: 更新1:

I have since changed the parameters to the following pattern: 此后,我将参数更改为以下模式:

var command = new SqlCommand("GenerateInviteKey", conn);
command.Parameters.Add(new SqlParameter("@inviterId", invite.InviterId));
command.Parameters.Add(new SqlParameter("@inviteeEmail", invite.InviteeEmail));
command.Parameters.Add(new SqlParameter("@inviteKey", ParameterDirection.Output));

and changed the procedure parameter in question to @inviterId bigint, which had the same result. 并将相关过程参数更改为@inviterId bigint,其结果相同。

You forgot to set the CommandType to CommandType.StoredProcedure 您忘记将CommandType设置为CommandType.StoredProcedure

So it just ends up passing the parameters in, then executing the procedure by name and never passing the parameters in to that call. 因此,它最终只会传入参数,然后按名称执行过程,并且永远不会将参数传入该调用。

In your procedure def, it defines @inviterid as an int and your parameter def is trying to coerce it into a SqlDbType.BigInt . 在过程def中,它将@inviterid定义为int而参数def试图将其强制转换为SqlDbType.BigInt Not entirely sure, but that could be the issue. 不确定,但这可能是问题所在。

Why not just let your code determine the types automatically: 为什么不让您的代码自动确定类型:

    var command = new SqlCommand("GenerateInviteKey", conn);
    command.Parameters.Add(new SqlParameter("@inviterId", invite.InviterId));
    command.Parameters.Add(new SqlParameter("@inviteeEmail", invite.InviteeEmail));
    command.Parameters.Add(new SqlParameter("@inviteKey", ParameterDirection.Output));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM