我一直在努力使存储过程中的构建与Register.aspx一起使用。我没有使用向导部分。因此编写我自己的代码。我不明白错误在哪里。如果我使用exec stored procedure
并发送相同的值,它将在Management Studio中执行。但在代码中它失败了。
以下是执行存储过程的代码:
using (SqlConnection myConnection = new SqlConnection (ConfigurationManager.ConnectionStrings["connection"].ToString()))
{
using (SqlCommand insertNewUsers = new SqlCommand("aspnet_Membership_CreateUser", myConnection))
{
insertNewUsers.CommandType = CommandType.StoredProcedure;
insertNewUsers.Parameters.Add("@ApplicationName", SqlDbType.NVarChar, 256).Value = AppName;
insertNewUsers.Parameters.Add("@UserName", SqlDbType.NVarChar, 256).Value = UserName.Text;
insertNewUsers.Parameters.Add("@Password ", SqlDbType.NVarChar, 128).Value = goHashPassword;
insertNewUsers.Parameters.Add("@PasswordSalt", SqlDbType.NVarChar, 128).Value = newSalt;
insertNewUsers.Parameters.Add("@Email", SqlDbType.NVarChar, 256).Value = Email.Text;
// insertNewUsers.Parameters.Add(("@LoweredEmail"), SqlDbType.NVarChar, 256).Value = (Email.Text).ToLower();
insertNewUsers.Parameters.Add("@PasswordQuestion", SqlDbType.NVarChar, 256).Value = DBNull.Value ;
insertNewUsers.Parameters.Add("@PasswordAnswer", SqlDbType.NVarChar, 128).Value = DBNull.Value;
insertNewUsers.Parameters.Add("@IsApproved", SqlDbType.Bit).Value = 1;
// insertNewUsers.Parameters.Add("@IsLockedOut", SqlDbType.NVarChar, 1).Value = 0;
insertNewUsers.Parameters.Add("@CurrentTimeUtc", SqlDbType.DateTime).Value = DateTime.UtcNow.Date;
insertNewUsers.Parameters.Add("@CreateDate", SqlDbType.DateTime).Value = DateTime.Today.ToLocalTime();
insertNewUsers.Parameters.Add("@UniqueEmail", SqlDbType.Int).Value = 0;
insertNewUsers.Parameters.Add("@PasswordFormat", SqlDbType.Int).Value = 0;
pID = insertNewUsers.Parameters.Add("@return_value", SqlDbType.Int);
pID.Direction = ParameterDirection.ReturnValue;
try
{
if (insertNewUsers.Connection.State == ConnectionState.Closed)
{
insertNewUsers.Connection.Open();
}
insertNewUsers.ExecuteNonQuery(); // HERE IS WHERE IT FAILS
rowCount = (Int32)insertNewUsers.Parameters["@return_value"].Value;
} // END TRY
catch (Exception ex)
{
ErrorMessage.Text = ex.StackTrace.ToString();
}
finally
{
myConnection.Dispose();
}
}
}
现在,如果我使用以下参数运行内置存储过程,则会运行并添加用户并创建成员资格
DECLARE @return_value int,
@UserId uniqueidentifier
EXEC @return_value = [dbo].[aspnet_Membership_CreateUser]
@ApplicationName = N'/',
@UserName = N'Tester',
@Password = N'cUW3NXKEEjiNkxOjhLXwhHFQApQ=',
@PasswordSalt = N'GYkMYFzkDn0vm4Li',
@Email = N'Tester@gmail.com',
@PasswordQuestion = NULL,
@PasswordAnswer = NULL,
@IsApproved = 1,
@CurrentTimeUtc = N'20131027',
@CreateDate = N'20131027' ,
@UniqueEmail = 0,
@PasswordFormat = 0,
@UserId = @UserId OUTPUT
SELECT @UserId as N'@UserId'
SELECT 'Return Value' = @return_value
GO
我已经用表检查了数据类型并且没问题。我不确定日期时间戳。在手动执行时,我只需要像我指定的那样给'yyyymmdd',但是在c#中我指定的是在代码中看到的。希望能找到一些帮助
答案 0 :(得分:1)
〜从技术上讲,你在DotNet代码中缺少一个参数。 (outputUserIdParam见下文)
〜实际上,您应该使用MembershipProvider API。
insertNewUsers.CommandType = CommandType.StoredProcedure;
insertNewUsers.Parameters.Add("@ApplicationName", SqlDbType.NVarChar, 256).Value = AppName;
insertNewUsers.Parameters.Add("@UserName", SqlDbType.NVarChar, 256).Value = UserName;
insertNewUsers.Parameters.Add("@Password ", SqlDbType.NVarChar, 128).Value = pwd;
insertNewUsers.Parameters.Add("@PasswordSalt", SqlDbType.NVarChar, 128).Value = string.Empty;
insertNewUsers.Parameters.Add("@Email", SqlDbType.NVarChar, 256).Value = Email;
// insertNewUsers.Parameters.Add(("@LoweredEmail"), SqlDbType.NVarChar, 256).Value = (Email.Text).ToLower();
insertNewUsers.Parameters.Add("@PasswordQuestion", SqlDbType.NVarChar, 256).Value = DBNull.Value;
insertNewUsers.Parameters.Add("@PasswordAnswer", SqlDbType.NVarChar, 128).Value = DBNull.Value;
insertNewUsers.Parameters.Add("@IsApproved", SqlDbType.Bit).Value = 1;
// insertNewUsers.Parameters.Add("@IsLockedOut", SqlDbType.NVarChar, 1).Value = 0;
insertNewUsers.Parameters.Add("@CurrentTimeUtc", SqlDbType.DateTime).Value = DateTime.UtcNow.Date;
insertNewUsers.Parameters.Add("@CreateDate", SqlDbType.DateTime).Value = DateTime.Today.ToLocalTime();
insertNewUsers.Parameters.Add("@UniqueEmail", SqlDbType.Int).Value = 0;
insertNewUsers.Parameters.Add("@PasswordFormat", SqlDbType.Int).Value = 0;
// Create parameter with Direction as Output (and correct name and type)
SqlParameter outputUserIdParam = new SqlParameter("@UserId", SqlDbType.UniqueIdentifier)
{
Direction = ParameterDirection.Output
};
insertNewUsers.Parameters.Add(outputUserIdParam);
pID = insertNewUsers.Parameters.Add("@return_value", SqlDbType.Int);
pID.Direction = ParameterDirection.ReturnValue;