从checklistbox向AccessDB插入多个项目

本文关键字:项目 插入 AccessDB checklistbox | 更新日期: 2023-09-27 18:09:52

我有一个列出员工编号的listcheckedbox,我希望能够将参加培训课程的每个员工添加到培训课程中。但是,当我尝试提交要插入到DB中的信息时,它只会添加所选员工中的一个。我如何让它提交一个以上的员工到一个类会话?

try
            {
                string cmdstring = "INSERT INTO [SESSION] (PrincipleName, Comments, SessionDate, SessionName, TellerNum) VALUES (@principle, @comments, @date, @session, @teller)";
                using (OleDbCommand cmd = new OleDbCommand(cmdstring, con))
                {
                    cmd.Parameters.AddWithValue("@principle", comboBox12.Text);
                    cmd.Parameters.AddWithValue("@comments", textBox3.Text);
                    cmd.Parameters.AddWithValue("@date", dateTimePicker1.Value);
                    cmd.Parameters.AddWithValue("@session", comboBox1.Text);
                    cmd.Parameters.AddWithValue("@teller", checkedListBox1.Text);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("Submitted Successfully");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed due to " + ex.Message);
            }

这是我从LarsTech答案后更新的代码

                    con.Open();
                    string cmdstring = "INSERT INTO [SESSION] (PrincipleName, Comments, SessionDate, SessionName, TellerNum) VALUES (@principle, @comments, @date, @session, @teller)";
                    foreach (int s in checkedListBox1.CheckedItems)
                    {
                        using (OleDbCommand cmd = new OleDbCommand(cmdstring, con))
                        {
                            cmd.Parameters.AddWithValue("@principle", comboBox12.Text);
                            cmd.Parameters.AddWithValue("@comments", textBox3.Text);
                            cmd.Parameters.AddWithValue("@date", dateTimePicker1.Value.ToShortDateString());
                            cmd.Parameters.AddWithValue("@session", comboBox1.Text);
                            cmd.Parameters.AddWithValue("@teller", s);
                            cmd.ExecuteNonQuery();
                        }
                    }
                            con.Close();
                            MessageBox.Show("Submitted Successfully");
                            textBox3.Clear();
                            checkedListBox1.ClearSelected();
                            comboBox1.Refresh();
                            comboBox12.Refresh();
                            dateTimePicker1.Refresh();

从checklistbox向AccessDB插入多个项目

您必须遍历CheckedItems集合:

foreach (string s in checkedListBox1.CheckedItems)
{
  using (OleDbCommand cmd = new OleDbCommand(cmdstring, con))
  {
    cmd.Parameters.AddWithValue("@principle", comboBox12.Text);
    cmd.Parameters.AddWithValue("@comments", textBox3.Text);
    cmd.Parameters.AddWithValue("@date", dateTimePicker1.Value);
    cmd.Parameters.AddWithValue("@session", comboBox1.Text);
    cmd.Parameters.AddWithValue("@teller", s);
    cmd.ExecuteNonQuery();
  }
}