将checklistbox中的所有选定值存储到数据库中

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

我在c#中制作一个windows窗体,其中checklistbox中的所有值将存储到数据库中。代码实际上是有效的:

  1. 只有最后检查的值重复存储在数据库中,就像下面的数据一样
  2. 雇员id (id)在表famhistory上不会作为外键出现,但它出现在表name上,就像下面的数据一样

表:famhistory

famid           famcon          id
30              stroke
31              stroke
32              stroke

表:name

eid             name
2010-0244       Jam Lagcao
谁能帮我解决一下我的问题?非常感谢您的帮助。 下面是我的c#代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Npgsql;
namespace WindowsFormsApplication1
{
    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
            this.Load += Form6_Load;
            button1.Click += button1_Click;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=021393;Database=postgres;");
            NpgsqlCommand cmd = new NpgsqlCommand("Insert into name(eid, name) Values (@eid, @name)", conn);
            cmd.Parameters.AddWithValue("@eid", textBox1.Text);
            cmd.Parameters.AddWithValue("@name", textBox2.Text);
            conn.Open();
            foreach (DataRowView view in checkedListBox1.CheckedItems)
            {
                NpgsqlCommand cmd2 = new NpgsqlCommand("Insert into famhistory (famcon) Values (@famcon)", conn);
                string value = view.Row[0].ToString();
                Console.WriteLine(view[checkedListBox1.ValueMember].ToString());  
                cmd2.Parameters.AddWithValue("@famcon", checkedListBox1.SelectedValue);
                cmd2.ExecuteNonQuery();  
            }
            cmd.ExecuteNonQuery();
            MessageBox.Show("Data has been saved");
            conn.Close();
        }
        private void Form6_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            string connstring = ("Server=localhost;Port=5432;User Id=postgres;Password=021393;Database=postgres;");
            NpgsqlConnection conn = new NpgsqlConnection(connstring);
            NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM condition", conn);
            cmd.CommandType = CommandType.Text;
            conn.Open();
            NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
            dt.TableName = "condition";
            da.Fill(dt);
            checkedListBox1.DataSource = dt;
            checkedListBox1.DisplayMember = "conname";
            checkedListBox1.ValueMember = "conname";
            checkedListBox1.BindingContext = new BindingContext();
            string[] condition = dt.Rows[0]["conname"].ToString().Split(',');
        }
    }
}

将checklistbox中的所有选定值存储到数据库中

将插入操作代码更改为famhistory表,如下所示

foreach (var item in checkedListBox1.CheckedItems)
{
  NpgsqlCommand cmd2 = new NpgsqlCommand("Insert into famhistory (famcon) Values (@famcon)", conn);
cmd2.Parameters.AddWithValue("@famcon", item.ToString());
cmd2.ExecuteNonQuery();  
}

不确定为什么在foreach语句中使用DataRowView view;这是错误的。

另外,请注意我使用item.ToString()而不是checkedListBox1.SelectedValue,这将使您只获得最后选择的值。