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();
}
}
}