【问题标题】:C# - How do I find the database key of a Listbox item?C# - 如何找到列表框项的数据库键?
【发布时间】:2018-03-24 14:17:45
【问题描述】:

我正在编写一个应用程序,其中员工的姓氏和名字显示在(排序的)列表框中 - 使用 sql 语句为选定的业务部门(在下面的示例中硬编码)。员工(Access)数据库的索引为员工编号。

当从列表框中选择项目时,我找不到确定员工编号的方法,该方法使我能够读取数据库以在新表单上获取有关员工的属性。我认为这个技巧可能使用 keyvaluepair 但需要一些关于如何编码的指导。代码示例如下,感谢您的帮助。

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private OleDbConnection connection = new OleDbConnection();
        public Form1()
        {
            InitializeComponent();
            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Mark\Documents\Employee.accdb;Persist Security Info=False;";
            listBox1_Load();
            }

    void listBox1_Load()
        {
            try
            {
            // Open database and select the data to be shown in the List Box - hard-coded example here
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query = "select ID, FirstName, Surname from Employee where BusinessUnit = 'Finance'";
            command.CommandText = query;

            OleDbDataReader reader = command.ExecuteReader();

            // Read records returned by the query and populate the list box with the employee name
            while (reader.Read())
            {
                listBox1.Items.Add(reader["Surname"] + ", " + reader["FirstName"]);
            }
            connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

【问题讨论】:

    标签: c# database indexing listbox


    【解决方案1】:

    不确定是否可以在列表框中添加隐藏字段。但是,如果您将收到的查询存储到保留的变量中(可能是数据表,而不是使用数据读取器),那么当用户单击以从列表框中选择一个选项时,您可以使用它来查找查询中的关联记录使用列表框选定项的索引。

    但是,您必须确保查询对结果进行排序,而不是列表框。

    迈克

    【讨论】:

    • 谢谢 Mike - 你的最后一点是我没有考虑过的,应该让我的生活更轻松。
    • 没问题,最好将列表框的数据源绑定到数据表并在列表框上设置 valuemember 和 displaymember 属性。
    【解决方案2】:

    不是一个合适的答案,而是一个想法:

    也许您可以尝试将结果转换为数组或字典之类的方法

    listBox1.DataSource = <Array or Dictionary>;
    listBox1.DisplayMember = "NameOfColumnToBeDisplayed";
    listBox1.ValueMember = "NameOfColumnToUseValueFrom";
    

    显示!= 值(例如,值将是您的员工 ID)

    【讨论】:

    • 显示的数据将多于一列,格式为“姓氏,名字”,所以我不确定 DisplayMember 是否允许这样做。我之前尝试过使用数组,但由于列表自动在列表框中排序,因此项目将不匹配。
    • 他可以将两列连接为一个字符串。这实际上取决于他的目的以及如何成功检索他可以轻松操作的集合。自从我上次接触 C# 以来已经有一段时间了,但我记得做过一些非常相似的事情。不过,这只是一个想法。
    【解决方案3】:

    我会将数据库中的数据存储在字典中,比如说

    Dictionary<int,String[]>
    

    其中键是 ID,数组由 Name 和 Surname 组成。 然后用 Dictionary 数据填充 listBox。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多