我在语句中收到异常Invalid column name 'False'
reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
我知道这可能与 SQL 的布尔类型有点“位”有关,它没有转换false
为0
. 您如何减轻这种情况,或者这就是问题所在?
public override bool ValidateUser(string username, string password)
{
bool isValid = false;
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT Password, IsApproved FROM Users" +
" WHERE Email = @Email AND ApplicationName = @ApplicationName AND IsLockedOut = False", conn);
cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 128).Value = username;
cmd.Parameters.Add("@ApplicationName", SqlDbType.NVarChar, 255).Value = m_ApplicationName;
SqlDataReader reader = null;
bool isApproved = true;
string pwd = "";
try
{
conn.Open();
reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
if (reader.HasRows)
{
reader.Read();
pwd = reader.GetString(0);
isApproved = reader.GetBoolean(1);
}
else
{
return false;
}
reader.Close();
if (CheckPassword(password, pwd))
{
if (isApproved)
{
isValid = true;
SqlCommand updateCmd = new SqlCommand("UPDATE Users SET LastLoginDate = @LastLoginDate" +
" WHERE Email = @Email AND ApplicationName = @ApplicationName", conn);
updateCmd.Parameters.Add("@LastLoginDate", SqlDbType.DateTime).Value = DateTime.Now;
updateCmd.Parameters.Add("@Email", SqlDbType.NVarChar, 255).Value = username;
updateCmd.Parameters.Add("@ApplicationName", SqlDbType.NVarChar, 255).Value = m_ApplicationName;
updateCmd.ExecuteNonQuery();
}
}
else
{
conn.Close();
UpdateFailureCount(username, "password");
}
}