存储数据库中的一些数据

本文关键字:数据 数据库 存储 | 更新日期: 2023-09-27 18:27:16

我用这个在comboBox中添加每个员工的全名。问题是我将EmpID存储(隐藏)在哪里,这样,如果在组合框中选择了一个项目,它将在文本框中显示所选员工的EmpID?

public void fillComboBox()
{
    comboBox1.Items.Add("Add Employee");
    using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
    {
        myDatabaseConnection.Open();
        using (SqlCommand mySqlCommand = new SqlCommand("Select EmployeeID, LastName, FirstName, MiddleName from Employee", myDatabaseConnection))
        {
            using (SqlDataReader sqlreader = mySqlCommand.ExecuteReader())
            {
                while (sqlreader.Read())
                {
                    string Lname = sqlreader.GetString(sqlreader.GetOrdinal("LastName"));
                    string Fname = sqlreader.GetString(sqlreader.GetOrdinal("FirstName"));
                    string Mname = sqlreader.GetString(sqlreader.GetOrdinal("MiddleName"));
                    string fullName = Lname + ", " + Fname + " " + Mname; 
                    comboBox1.Items.Add(fullName);
                }
            }
        }
    }
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //textBox1.Text = SelectedEmpID
        if (comboBox1.Text == "Add Employee")
        {
            EmployeeForm nf = new EmployeeForm();
            DialogResult res = nf.ShowDialog();
            if (res == DialogResult.OK)
            {
                comboBox1.Items.Clear();
                fillComboBox();
            }
        }
    }

存储数据库中的一些数据

这样做

using (SqlConnection con = new SqlConnection(myConnectionString.ConnectionString))
    {
        string query = "Select EmployeeID, (LastName + ','+ FirstName+' '+ MiddleName) as Name from Employee";
        SqlDataAdapter dap = new SqlDataAdapter(query ,con);
        DataTable dt = new DataTable();
        dap.Fill(dt);
         comboBox1.DisplayMember = "Name";
         comboBox1.ValueMember = "EmployeeID";
        DataRow dr = dt.NewRow();
        dr[0] = -1;
        dr[1] = "Add Employee";
        dt.Rows.InsertAt(dr, 0);
        comboBox1.DataSource = dt;
    }

然后

   private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
   {
       if(Convert.ToInt32(comboBox1.SelectedValue.ToString())==-1)
       {
        EmployeeForm nf = new EmployeeForm();
        DialogResult res = nf.ShowDialog();
        if (res == DialogResult.OK)
        {
            comboBox1.Items.Clear();
            fillComboBox();
        }
       }
    }

注意:如果您只想在用户更改组合框中所选项目时做出反应,那么最好订阅SelectionChangeCommitted。

首先,我将创建一个类Employee。例如:

public class Employee
{
    public int EmployeeID {get;set;}
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string FullName { get { return LastName + ", " + FirstName + " " + MiddleName; } }
    public Employee(IDataRecord record)
    {
        this.EmployeeID = Convert.ToInt32(record["EmployeeID"]);
        this.LastName = record["LastName"].ToString();
        this.FirstName = record["FirstName"].ToString();
        this.MiddleName = record["MiddleName"].ToString();
    }
}

使用该类,fillBox()函数将如下所示:

 public void fillComboBox()
    {
        comboBox1.DisplayMember = "FullName";
        comboBox1.Items.Add("Add employee");
        using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
        {
            myDatabaseConnection.Open();
            using (SqlCommand mySqlCommand = new SqlCommand("Select EmployeeID, LastName, FirstName, MiddleName from Employee", myDatabaseConnection))
            {
                using (SqlDataReader sqlreader = mySqlCommand.ExecuteReader())
                {
                    while (sqlreader.Read())
                    {
                        comboBox1.Items.Add(new Employee(sqlreader));
                    }
                }
            }
        }
    }

当然还有SelectedIndexChangedEvent然后:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
          var selecetedEmployee = comboBox1.SelectedItem as Employee;
          if (selecetedEmployee == null)
          {
               EmployeeForm nf = new EmployeeForm();
               DialogResult res = nf.ShowDialog();
               if (res == DialogResult.OK)
               {
                   comboBox1.Items.Clear();
                   fillComboBox();
               }
           }
           else
           {
                textBox1.Text = selecetedEmployee.EmployeeID.ToString();
           }
      }