0
내 사용자 데이터베이스에 맞게 일부 사용자 지정 멤버 자격 공급자를 msdn.microsoft.com/en-us/library/ms366730.aspx에서 사용했습니다. GetUser 메서드에서 SqlDataReader를 사용하여 다음을 throw합니다.SqlDataReader 예외 : 인덱스가 배열 경계 외부에 있습니다.
System.IndexOutOfRangeException : 인덱스가 배열 경계를 벗어났습니다.
배열의 크기는 어떻게 결정됩니까? 나는 그것이 SQL SELECT 문이라고 가정하고 있으며, 따라서 12 개의 항목이 있으므로 reader.GetBoolean(11)
이 마지막 색인이어야한다. 맞습니까?
private NCCMembershipUser GetUserFromReader(SqlDataReader reader)
{
object providerUserKey = reader.GetValue(0);
string username = reader.GetString(1);
string email = username;
string passwordQuestion = "";
string comment = "";
// 0 UserID, 1 Email, 2 PasswordQuestion,
// 3 Comment, 4 IsApproved, 5 IsLockedOut, 6 CreationDate, 7 LastLoginDate,
// 8 LastActivityDate, 9 LastPasswordChangedDate, 10 LastLockedOutDate, 11 IsSubscribed
bool isApproved = reader.GetBoolean(4);
bool isLockedOut = reader.GetBoolean(5);
bool isSubscribed = reader.GetBoolean(11);// <--- ****HERE****
DateTime creationDate = reader.GetDateTime(6);
DateTime lastLoginDate = new DateTime();
DateTime lastActivityDate = reader.GetDateTime(8);
DateTime lastPasswordChangedDate = reader.GetDateTime(9);
DateTime lastLockedOutDate = new DateTime(10);
if (reader.GetValue(2) != DBNull.Value)
passwordQuestion = reader.GetString(2);
if (reader.GetValue(3) != DBNull.Value)
comment = reader.GetString(3);
if (reader.GetValue(7) != DBNull.Value)
lastLoginDate = reader.GetDateTime(7);
if (reader.GetValue(10) != DBNull.Value)
lastLockedOutDate = reader.GetDateTime(10);
NCCMembershipUser u = new NCCMembershipUser(this.Name,
username,
providerUserKey,
email,
passwordQuestion,
comment,
isApproved,
isLockedOut,
creationDate,
lastLoginDate,
lastActivityDate,
lastPasswordChangedDate,
lastLockedOutDate,
isSubscribed);
return u;
}
이 방법은 인 getUser에서 호출 :
public override MembershipUser GetUser(string username, bool userIsOnline)
{
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT UserID, Email, PasswordQuestion," +
" Comment, IsApproved, IsLockedOut, CreationDate, LastLoginDate," +
" LastActivityDate, LastPasswordChangedDate, LastLockedOutDate" +
" IsSubscribed" +
" 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();
}
}
}
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;
}
놀라운 토요일을 한 캐릭터가 어떻게 망칠 수 있는지 놀라운. 감사! – RyanJMcGowan