【问题标题】:SQL Error: Procedure or function 'CreateUserAccount' expects parameter '@FirstName', which was not suppliedSQL 错误:过程或函数“CreateUserAccount”需要参数“@FirstName”,但未提供该参数
【发布时间】:2015-01-25 11:58:39
【问题描述】:

我已经调试了代码,从我看到的情况来看,我确实为我的参数添加了值,但是任何人都可以帮助解决这个问题吗?我认为我看不出任何问题,但我的另一双眼睛可能会有所帮助。

方法

    public bool CreateUserAccount(string firstName, string lastName, string aboutUser, string email, string password, 
        string addressLine1, string addressLine2, string city, string postcode, string contactNumber)
    {
        bool registered = false;
        try
        {
            connection = OpenSqlConnection();

            command = new SqlCommand(CREATE_USER_ACCOUNT, connection);
            command.Parameters.AddWithValue(FIRST_NAME, firstName);
            command.Parameters.AddWithValue(LAST_NAME, lastName);
            command.Parameters.AddWithValue(ABOUT_USER, aboutUser);
            command.Parameters.AddWithValue(USER_EMAIL, email);
            command.Parameters.AddWithValue(USER_PASSWORD, password);
            command.Parameters.AddWithValue(ADDRESS_LINE_1, addressLine1);
            command.Parameters.AddWithValue(ADDRESS_LINE_2, addressLine2);
            command.Parameters.AddWithValue(CITY, city);
            command.Parameters.AddWithValue(POSTCODE, postcode);
            command.Parameters.AddWithValue(CONTACT_NUMBER, contactNumber);

            registered = Convert.ToBoolean(command.ExecuteNonQuery());
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return registered;
    }

SQL

ALTER PROC CreateUserAccount
(
@FirstName VARCHAR(50),
@LastName VARCHAR(50),
@AboutUser VARCHAR(3000),
@UserEmail VARCHAR(200),
@UserPassword VARCHAR(50),
@AddressLine1 VARCHAR(50),
@AddressLine2 VARCHAR(50),
@City VARCHAR(50),
@Postcode VARCHAR(50),
@ContactNumber VARCHAR(50)
)
AS BEGIN
DECLARE @Registered INT
IF(NOT EXISTS(SELECT * FROM [User] WHERE usr_Email = @UserEmail))
BEGIN
    INSERT INTO [User]
    VALUES
        (
            NEWID(),
            @FirstName,
            @LastName,
            @AboutUser,
            @UserEmail,
            @UserPassword,
            @AddressLine1,
            @AddressLine2,
            @City,
            @Postcode,
            @ContactNumber
        )
    SET @Registered = 1
    PRINT 'Implemented' -- for testing purposes
END
ELSE
BEGIN
    SET @Registered = 0
    PRINT 'Not implemented' -- for testing purposes
END
RETURN @Registered
END

【问题讨论】:

  • 不看你的代码我猜你忘了在 SqlCommand 中指定 CommandType
  • 看了你的代码,我想我是对的
  • 另外,我们不知道您的常量 FIRST_NAME 设置为什么。
  • 您应该查看Can we stop using AddWithValue() already? 并停止使用.AddWithValue() - 它可能会导致意想不到和令人惊讶的结果...

标签: c# asp.net sql-server exception ado.net


【解决方案1】:

试试这个(注意 CommandType)

public bool CreateUserAccount(string firstName, string lastName, string aboutUser, string email, string password, 
    string addressLine1, string addressLine2, string city, string postcode, string contactNumber)
{
    bool registered = false;
    try
    {
        connection = OpenSqlConnection();

        command = new SqlCommand(CREATE_USER_ACCOUNT, connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddWithValue(FIRST_NAME, firstName);
        command.Parameters.AddWithValue(LAST_NAME, lastName);
        command.Parameters.AddWithValue(ABOUT_USER, aboutUser);
        command.Parameters.AddWithValue(USER_EMAIL, email);
        command.Parameters.AddWithValue(USER_PASSWORD, password);
        command.Parameters.AddWithValue(ADDRESS_LINE_1, addressLine1);
        command.Parameters.AddWithValue(ADDRESS_LINE_2, addressLine2);
        command.Parameters.AddWithValue(CITY, city);
        command.Parameters.AddWithValue(POSTCODE, postcode);
        command.Parameters.AddWithValue(CONTACT_NUMBER, contactNumber);

        registered = Convert.ToBoolean(command.ExecuteNonQuery());
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return registered;
}

另外,确保您的 FIRST_NAME 变量/常量设置为 FirstName@FirstName

【讨论】:

  • 是的,谢谢,我错过了命令类型,不知道我是怎么错过的……我的常数是正确的。再次感谢。
  • 这很容易做到 :) 我以前经常这样做,但我对自己的错误很明智!
  • 是的 :) 可能是因为我凌晨 1 点在伦敦编程..半睡着
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 2020-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多