存储数据库中的一些数据

时间:2013-08-12 11:55:43

标签: c# winforms sql-server-2008-r2

我有这个在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();
            }
        }
    }

2 个答案:

答案 0 :(得分:3)

这样做

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。

答案 1 :(得分:0)

首先,我会创建一个类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();
           }
      }