从c#中获取SQL Server数据表中的单个值

时间:2017-10-19 02:08:07

标签: c# sql-server database sql-injection password-protection

我正在创建一个Windows登录表单,其中包含一个选项,以防您忘记密码,在该表单中,您将点击一个按钮将您引导至另一个按钮,您可以在其中回答安全问题和内容以确认您的身份,然后再检索密码从数据表中显示并显示在MessageBox中。我的代码已经能够验证身份,但如何从输入用户名的同一行获取密码值?

我绝对没有编程背景和知识,如果你能帮助我们提供示例或代码,我将非常感谢:)

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Daniel Koh\Documents\AccountData.mdf;Integrated Security=True;Connect Timeout=30");

    SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From [Table] where Username='" + textBox1.Text + "' and EmployeeId ='" + textBox2.Text + "' and SecurityQuestionAnswer='" + textBox3.Text + "' and SecurityQuestionType='" + comboBox1.SelectedItem.ToString() + "'", con);

    DataTable dt = new DataTable();
    sda.Fill(dt);

    SqlConnection.ClearAllPools();

    if ((dt.Rows[0][0].ToString() == "1"))
    {
        string username = textBox1.Text;

        string password = (from DataRow dr in dt.Rows
                           where (string)dr["username"] == username
                           select (string)dr["Password"]).FirstOrDefault();
        MessageBox.Show(password);
    }
    else
    {
        MessageBox.Show("Please check your Username, Security Question Answer and EmployeeID");
    }
}

0 个答案:

没有答案