【发布时间】:2021-08-14 16:58:05
【问题描述】:
我在 SQL Server 中有一个表 EmployeeRank1,其中有一列 Name。在Name 列下,有两个预定义的员工姓名。此外,表格中有一列Password,其中包含一个通用密码,即“123456”。
在 WPF 中,我有一个要求输入姓名的文本框和一个要求输入密码的密码框。在它们下方,有一个显示“登录”的按钮。
问题是我如何将表格中Name和Pasword的内容与文本框和密码框中的输入进行比较?
如果输入的Name 存在且Password 正确,则会打开一个新的WPF 页面。否则,将打印一条消息,说明名称或密码不正确。
这是我到现在为止的:
// check if the input matches and open the new WPF Page
private void EmployeeRank1Button_Click(object sender, RoutedEventArgs e)
{
try
{
// create a query and select everything from the EmployeeRank1 table
string query = "select * from EmployeeRank1";
// create a connection to the database and run the query
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(query, sqlConnection);
// use the sqlDataAdapter
using(sqlDataAdapter)
{
// create a new DataTable that allows us
// to store data from tables within objects
DataTable employeeRank1Table = new DataTable();
// fill the sqlDataAdapter with all the
// information from the query(from the employeeRank1Table)
sqlDataAdapter.Fill(employeeRank1Table);
// TODO: compare Name and Password entered in the TextBox and PasswordBox to the data in the table
if (tbName.Text == *Name in Table* && pbPassword.Password == *Password in Table*)
{
EmployeeRank1 employeeRank1 = new EmployeeRank1();
employeeRank1.Show();
}
}
}
catch(Exception exception)
{
MessageBox.Show(exception.ToString());
}
}
【问题讨论】:
-
使用这样的东西:DataTable filteredTable = employeeRank1Table.AsEnumerable().Where(x => x.Field
("column Name") == 123).CopyToDataTable();跨度> -
您应该从不 _ _ _ EVER 以明文的形式将您的密码存储在数据库表中 - 只需 不要这样做 - 句号。
标签: c# sql-server database wpf