gpt4 book ai didi

c# - 如何在文本框中显示选定的记录

转载 作者:行者123 更新时间:2023-11-29 07:23:31 27 4
gpt4 key购买 nike

因此,我尝试将表中选定的记录放入文本框中,更具体地说,是将用户的名字及其 ID 放入文本框中。

textBox2 显示名字,textBox3 显示 ID。从技术上讲,它是有效的,但它始终显示表中的最后一条记录,而不是用户用于登录的特定记录。

例如,textBox2 显示“Bob”,textBox3 显示“2”,而按理说它们应该显示“Bill”和“1”。

预先感谢您的帮助。

void login()
{
string firstName;
string studentID;

string path = "Data Source = LOCALHOST; Initial Catalog =library ; username='root';password=''";
MySqlConnection con = new MySqlConnection(path);
string sqlSelect = "select * from users";
MySqlCommand sqlCommand = new MySqlCommand(sqlSelect, con);
try
{
con.Open();
using (MySqlDataReader sqlReader = sqlCommand.ExecuteReader())
{
while (sqlReader.Read())
{
firstName = sqlReader["FirstName"].ToString();
studentID = sqlReader["ID"].ToString();
textBox2.Text = firstName;
textBox3.Text = studentID;
}
}
}
catch
{
con.Close();
}
}

第一种表单中的日志记录代码。

void login()
{
string userName;
string password;
string userType;

string path = "Data Source = LOCALHOST; Initial Catalog =library ; username='root';password=''";
MySqlConnection con = new MySqlConnection(path);
string sqlSelect = "select * from users WHERE Username = '" + textBox1.Text + "' AND Password = '" + textBox2.Text + "'";
MySqlCommand sqlCommand = new MySqlCommand(sqlSelect, con);

try
{
con.Open();
using (MySqlDataReader sqlReader = sqlCommand.ExecuteReader())
{
while (sqlReader.Read())
{
userName = sqlReader["Username"].ToString();
password = sqlReader["Password"].ToString();
userType = sqlReader["UserType"].ToString();

if (textBox1.Text == userName)
{
if (textBox2.Text == password)
{
if (comboBox1.Text == userType)
{
if (userType == "Admin")
{
MessageBox.Show("Logged in as Admin", "Login");
Admin frmAdmin = new Admin();
this.Hide();
frmAdmin.Show();
}
else if (userType == "Student")
{
MessageBox.Show("Logged in as Student", "Login");
Student frmStudent = new Student();
this.Hide();
frmStudent.Show();
}
}
}
}
}
}
}
finally
{
con.Close();
}


}

最佳答案

您正在从表中选择所有用户。您需要一些 WHERE 逻辑来将该结果集限制为 1 行。否则,您的 while 循环将迭代直到返回最后一条记录,这些值将是显示的值。

关于c# - 如何在文本框中显示选定的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35283127/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com