将所有选中的值从checkedlistbox存储到数据库

时间:2015-08-04 01:48:56

标签: c# npgsql checkedlistbox

我在c#中创建一个windows表单,其中checkedlistbox中的所有值都将存储到数据库中。但是代码确实有效:

  1. 仅检查的最后一个值是在数据库上重复存储,就像下面的数据一样
  2. 表格famhistory上的
  3. 员工ID(id)不会显示为外键,但它会显示在表name上,就像下面的数据一样
  4. 表: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(',');
    
            }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

将插入操作代码更改为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();  
}

不确定您在DataRowView view声明中使用foreach的原因;那是错的。

此外,请参阅我使用的是item.ToString()而不是checkedListBox1.SelectedValue,它只会为您提供最后一个选定的值。