0

reader = cmd.ExecuteReader();调试时抛出异常。

CS0266:无法将类型“System.IAsyncResult”隐式转换为“System.Data.SqlClient.SqlDataReader”。

这是从 MSDN 示例成员资格提供程序 (http://msdn.microsoft.com/en-us/library/6tc47t75.aspx) 派生的自定义 MembershipProvider,其用户名值调用电子邮件数据库字段而不是单独的用户名条目,并且连接已从 Odbc 更改为 SQL 连接。

    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" +
            " FROM Users WHERE Email = ? AND ApplicationName = ?", conn);

        cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 128).Value = username;
        cmd.Parameters.Add("@ApplicationName", SqlDbType.NVarChar, 255).Value = m_ApplicationName;

        MembershipUser 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 = ? " +
                        "WHERE Email = ? AND 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();
                }
            }

        }
4

2 回答 2

1

您是否尝试过在 sqlcommand 中指定参数名称?问号对我来说似乎很奇怪

SqlCommand cmd = new SqlCommand("SELECT UserID, Email, PasswordQuestion," +
    " Comment, IsApproved, IsLockedOut, CreationDate, LastLoginDate," +
    " LastActivityDate, LastPasswordChangedDate, LastLockedOutDate" +
    " FROM Users WHERE Email = @email AND ApplicationName = @applicationname", conn);
于 2012-05-31T02:19:11.020 回答
0

您是否尝试过使用 Dataadaptor 而不是阅读器?无论如何,您都没有正确关闭阅读器,这可能会导致您的网站出现内存问题。老实说,除了非常具体的情况外,我几乎总是与读者有问题。

于 2012-05-31T02:18:48.090 回答