【发布时间】:2020-06-15 18:29:09
【问题描述】:
如果正确回答了两个安全问题,我的 WPF 项目中有一个窗口可以帮助用户显示忘记的密码。不幸的是,我的代码不能正常工作,因此我需要你的帮助。
在我的代码中,我尝试从文本框中读取第一个和第二个答案,如果它们与 SQL Server 表中的 S_Question1 和 S_Question2 匹配,那么应该显示Psswrd,将相同的字段写入记录。
非常感谢您的温柔反馈!
我的 XML 代码:
<StackPanel HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.Row="1">
<TextBox x:Name="SecurtyQuestionMother" Width="385"/>
<TextBox x:Name="SecurityQuestionSchool" Width="385" Margin="0 10 0 0"/>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center"
Margin="10">
<Button Name="match"
Content="match"
Width="50"
Click="CheckPassword"/>
</StackPanel>
<Label x:Name="PasswordApears"/>
</StackPanel>
后面的代码:
private void CheckPassword(object sender, RoutedEventArgs e) //updated...
{
SqlConnection conn = new SqlConnection(connectionString);
string sql = "select * from Ceo where S_Question1 =@one and S_Question2=@two;";
SqlCommand command = new SqlCommand(sql, conn);
command.Parameters.AddWithValue("@one", securityQ_mother_textbox.Text).SqlDbType = SqlDbType.Char;
command.Parameters.AddWithValue("@two", securityQ_school_texbox.Text).SqlDbType = SqlDbType.Char;
conn.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
if ((reader["S_Question1"]).ToString() == securityQ_mother_textbox.Text || reader["S_Question2"].ToString() == SecurityQuestionSchool.Text)
{
PasswordApears.Content = $"Password is: {reader["Psswrd"]}"; //PasswordApears <- is a label
}
else
{
PasswordApears.Content = "Didn't match";
}
}
conn.Close(); }
【问题讨论】:
-
你能做一些调试并验证问题出在哪里吗?阅读器中的值是否正确?它甚至正确连接到数据库吗? (如果您输入的是小写字母,可能是大小写问题?)(另外,您使用 or 和 not and 进行检查。
-
非常感谢您的回复,它已连接,但仅显示一个错误(严重性代码描述项目文件行抑制状态警告 CS0108 'AddCustomer.Close' 隐藏继承的成员 'Window.Close()' . 如果要隐藏,请使用新关键字。SoleTrader .......\SoleTrader\Dialogs\AddCustomer.xaml 293 Active )
-
与您的问题无关,但仅供参考:您存储密码的方式非常不安全。
-
让我们想想,不是密码,而是另一个字段。
-
哦...问题是您的 SELECT 语句。您只需选择 Psswrd 字段。所以 reader[6] 不存在。要么对所有字段使用 SELECT *,要么只选择 S_Question、S_Question2,然后是 reader[0] 和 reader[1]。不知道我第一次怎么错过了对不起!您还应该对 ID 字段进行 WHERE 检查,因为这是唯一的。如果您有 2 个用户的安全答案相同,则您无法确定会返回哪一个。