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

标签 c# mysql

因此,我尝试将表中选定的记录放入文本框中,更具体地说,是将用户的名字及其 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/

相关文章:

c# - asp.net/C#中请求的来源

c# - CaSTLe 3.0 如何删除组件?

c# - 将 SQL 查询转换为 LINQ C#

c# - 阵列等的磁盘存储

mysql - 基于上次更新的组的 SQL 查询

mysql - SQL查询中的多条件if语句

mysql - 像这样的 SQL 查询 : "some string" LIKE "%"+column +"%"

php - WordPress 调用数据库查询的问题

MySQL 数据库结构 : 1 Device consists of multiple Components

c# - Linq 合并查询