调用成员资格提供程序方法的正确方法
本文关键字:方法 程序 成员 调用 | 更新日期: 2023-09-27 18:37:02
在我的CreateUserWizard上,我正在调用其他属性,当我调用GetUser()时,我收到:
"已经有一个与此命令关联的打开的 DataReader,必须先关闭"
注册的代码隐藏.aspx:
NCCMembershipUser currentUser = (NCCMembershipUser)Membership.GetUser();
Guid id = (Guid)currentUser.ProviderUserKey;
...assigning control values to membership values...
try
{
NCCMembershipProvider u = (NCCMembershipProvider)Membership.Provider;
u.UpdateUser(currentUser);
}
我不应该使用 GetUser 来访问属性吗?
下面是 GetUser 方法:
public override MembershipUser GetUser(string username, bool userIsOnline)
{
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT UserID," +
" Email," +
" Comment," +
" PasswordQuestion," +
" IsApproved," +
" LastActivityDate," +
" LastLoginDate," +
" LastPasswordChangedDate," +
" CreationDate," +
" IsLockedOut," +
" LastLockedOutDate," +
" UserSalutation," +
" UserFirstName," +
" UserLastName," +
" UserPosition," +
" UserCompany," +
" UserCompanyType," +
" UserCompanyTypeOther," +
" UserAccountType," +
" UserAddress1," +
" UserAddress2," +
" UserCity," +
" UserStateProv," +
" UserPostal," +
" UserCountry," +
" UserWebsite," +
" UserPhone," +
" UserPhoneExt," +
" UserFax," +
" UserIP," +
" UserIPLastLogin," +
" IsSubscribed," +
" LikeAreaRugs," +
" LikeCarpeting," +
" LikeCoverings," +
" LikeComponents," +
" LikeHotel," +
" LikeAccessories" +
" FROM Users WHERE Email = @Email AND ApplicationName = @ApplicationName", conn);
cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 128).Value = username;
cmd.Parameters.Add("@ApplicationName", SqlDbType.NVarChar, 255).Value = m_ApplicationName;
NCCMembershipUser u = null;
SqlDataReader reader = null;
try
{
conn.Open();
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
u = GetUserFromReader(reader);
if (userIsOnline)
{
SqlCommand updateCmd = new SqlCommand("UPDATE Users " +
"SET LastActivityDate = @LastActivityDate " +
"WHERE Email = @Email AND ApplicationName = @ApplicationName", conn);
updateCmd.Parameters.Add("@LastActivityDate", SqlDbType.DateTime).Value = DateTime.Now;
updateCmd.Parameters.Add("@Email", SqlDbType.VarChar, 255).Value = username;
updateCmd.Parameters.Add("@ApplicationName", SqlDbType.VarChar, 255).Value = m_ApplicationName;
updateCmd.ExecuteNonQuery();//<<------Error:"There is already an open DataReader associated with this Command which must be closed first."
}
}
}
catch (SqlException e)
{
if (WriteExceptionsToEventLog)
{
WriteToEventLog(e, "GetUser(String, Boolean)");
throw new ProviderException(exceptionMessage);
}
else
{
throw e;
}
}
finally
{
if (reader != null) { reader.Close(); }
conn.Close();
}
return u;
}
在发出更新语句之前,您需要Close
读取器。
try
{
conn.Open();
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
u = GetUserFromReader(reader);
reader.Close();
if (userIsOnline)
{
SqlCommand updateCmd = new SqlCommand("UPDATE Users " +
"SET LastActivityDate = @LastActivityDate " +
"WHERE Email = @Email AND ApplicationName = @ApplicationName", conn);
updateCmd.Parameters.Add("@LastActivityDate", SqlDbType.DateTime).Value = DateTime.Now;
updateCmd.Parameters.Add("@Email", SqlDbType.VarChar, 255).Value = username;
updateCmd.Parameters.Add("@ApplicationName", SqlDbType.VarChar, 255).Value = m_ApplicationName;
updateCmd.ExecuteNonQuery();
}
}
else
{
reader.Close()
}
}