SQL 错误:过程或函数“创建用户帐户”需要参数“@FirstName”,但未提供
本文关键字:@FirstName 参数 过程 错误 函数 用户 创建 SQL | 更新日期: 2023-09-27 18:32:14
我已经调试了代码,从我所看到的情况来看,我确实添加了带有值的参数,但是任何人都可以帮助解决这个问题吗?我不认为我能看到任何问题,但我的另一双眼睛可能会有所帮助。
方法
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
试试这个(注意命令类型)
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