【发布时间】: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