簡體   English   中英

SqlDataReader異常:索引超出了數組的范圍

[英]SqlDataReader exception: Index was outside the bounds of the array

我使用了msdn.microsoft.com/en-us/library/ms366730.aspx上的自定義成員資格提供程序,並進行了一些更改以適合我的用戶數據庫。 在GetUser方法中,它使用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;
    }

我注意到你有你的SQL沒有,LastLockedOutDate和IsSubscribed之間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM