【问题标题】:Displaying the values of listview in textboxes在文本框中显示列表视图的值
【发布时间】:2023-03-24 15:38:01
【问题描述】:

我正在 Visual Studio 2010 C# 和 MySQL 中创建一个应用程序,用户可以在其中添加、编辑、查看员工。我已经完成了添加和查看部分。但是我在编辑时有点困惑。我的表单中有这个 listView,它显示了添加到数据库中的所有员工。我想要的是,每当用户选择员工并单击编辑按钮时,我希望保存在数据库中的值显示在 listView 下方的相应文本框中。有人可以告诉我如何做到这一点吗?请

截图:

listView 的代码:

private void getEmployee()
    {
        listViewEmployee.Items.Clear();
        string cmd = "select employee_number, employee_lastname, employee_firstname, employee_middlename, employee_position, employee_datehired from employee";
        DBConn db = new DBConn();
        DataTable tbl = db.retrieveRecord(cmd);
        foreach (DataRow row in tbl.Rows)
        {
            ListViewItem lv = new ListViewItem(row[0].ToString());
            lv.SubItems.Add(row[1].ToString() + ", " + row[2].ToString() + " " + row[3].ToString());
            lv.SubItems.Add(row[4].ToString());
            lv.SubItems.Add(row[5].ToString());
            listViewEmployee.Items.Add(lv);
        }
    }

    private void textBoxSearchEmployee_TextChanged(object sender, EventArgs e)
    {
        string cmd = "SELECT employee_number, employee_lastname, employee_firstname, employee_middlename, employee_position, employee_datehired FROM employee where employee_lastname Like '" + textBoxSearchEmployee.Text + "%'";
        listViewEmployee.Items.Clear();
        DBConn db = new DBConn();
        DataTable tbl = db.retrieveRecord(cmd);
        foreach (DataRow row in tbl.Rows)
        {
            ListViewItem lv = new ListViewItem(row[0].ToString());
            lv.SubItems.Add(row[1].ToString() + ", " + row[2].ToString() + " " + row[3].ToString());
            lv.SubItems.Add(row[4].ToString());
            lv.SubItems.Add(row[5].ToString());
            listViewEmployee.Items.Add(lv);
        }
    }

【问题讨论】:

  • 请提供一些相关代码。你的问题很广泛。当你说“我已经完成了添加和查看部分”时,你的意思是你已经能够用数据填充上面的文本框了吗?
  • 感谢您的编辑。但是,employee 表是什么样子的?它是否包含您需要的所有信息?或者您的数据库是否足够规范化,以至于其余信息都位于其他表中?
  • 该表格将仅显示一些重要的详细信息,例如员工编号、员工姓名、职位和雇用日期。但是当我单击编辑按钮时,我希望数据库中的所有数据都显示在文本框中。这可能吗?
  • 那么其他数据(例如税号、SSS 号等)保存在哪里?在另一张桌子上?
  • 仅在数据库中。我是否需要在表格中包含所有项目才能实现我想要的?

标签: c# mysql visual-studio-2010 listview textbox


【解决方案1】:

在我看来,您只能填充listView

几点建议:

1) 您现在编写 SQL 语句的方式很容易出现SQL Injection。在 SQL 命令中使用参数,而不是将变量直接连接到查询中。请参阅this question 了解如何操作的示例。

2) 根据您的其他相关数据所在的位置(即,如果您的数据库已标准化),您可能需要在查询中进行连接。

string sqlQuery = "SELECT * FROM employee 
JOIN other_employee_data_table on other_employee_data_table.employeeID = employee.ID
WHERE employee.employee_lastname LIKE @employee_lastname +'%'"

但如果您的employee 表包含所有数据,则无需进行连接。只需从该表中获取所有相关信息。

3) 获得所需的所有信息后,只需读取这些数据并将它们分配到各自的字段即可。

伪代码:

using (MySqlConnection connection = new MySqlConnection(connectionString))
{
   connection.Open(); 
   MySqlCommand command = connection.CreateCommand();
   command.CommandText = sqlQuery;
   command.CommandType = System.Data.CommandType.Text;
   // add parameters in this line
   using (MySqlDataReader reader = command.ExecuteReader())
   {  
      while (reader.Read()) 
      {     
         // iterate in each row
         for (int i = 0; i < reader.FieldCount; i++)
         {
            // iterate each column using reader.GetValue(i)
         }
      }
   }
}

【讨论】:

    【解决方案2】:

    当用户按下“编辑”按钮时...

    1. 检索选定的员工
    2. 使用SELECT 获取所选员工的信息
    3. 用所选员工的信息填充文本框

    例如,

    String employee = listViewEmployee.Text;
    String cmd = "SELECT * FROM employee WHERE employee_lastname='" + employee + "'";
    DBConn db = new DBConn();
    DataTable tbl = db.retrieveRecord(cmd);
    txtLastName.Text = tbl.Rows[0][0];
    // ... 
    // etc.
    

    注意:将值连接到 SQL 查询中是一个坏主意,因为如果这些值是恶意的,则可能会执行不同的查询。例如,如果employee 的值为x' OR '1'='1; DROP TABLE employee; -- 或类似的值,则可以删除员工表。解决这个问题的方法是使用Stored Procedures 或参数化查询。

    【讨论】:

    • @Alex R.:不是真的。员工姓名来自一个列表框,其值由 OP 填充。尽管如此,这只是示例代码,这就是他提出问题的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 2021-11-20
    • 2014-02-06
    • 1970-01-01
    相关资源
    最近更新 更多