C# SQL错误:过程或函数';CreateUserAccount';应为参数'@名字';,这是没有提供的
我已经调试了代码,从我看到的情况来看,我的参数确实添加了值,但是有人能帮我解决这个问题吗?我想我看不出有什么问题,但我想另一双眼睛可能会有所帮助 方法C# SQL错误:过程或函数';CreateUserAccount';应为参数'@名字';,这是没有提供的,c#,asp.net,sql-server,exception,ado.net,C#,Asp.net,Sql Server,Exception,Ado.net,我已经调试了代码,从我看到的情况来看,我的参数确实添加了值,但是有人能帮我解决这个问题吗?我想我看不出有什么问题,但我想另一双眼睛可能会有所帮助 方法 public bool CreateUserAccount(string firstName, string lastName, string aboutUser, string email, string password, string addressLine1, string addressLine2, strin
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
试试这个(注意命令类型)
另外,确保您的
FIRST_NAME
变量/常量设置为FirstName
或@FirstName
,而不阅读您的代码。我猜您忘记在SQLCommands中指定CommandType了。在阅读了您的代码之后,我猜我也更正了,我们不知道您的常量FIRST\u NAME
设置为什么。您应该签出并停止使用.AddWithValue()
-这可能会导致意外的结果…是的,谢谢,我错过了命令类型,不知道我是如何错过的。。。我的常数是对的。再次感谢。这很容易做到:)我过去经常这样做,但我已经认识到我的错误!是的:)可能是因为我凌晨1点在伦敦编程……半睡半醒
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;
}