在数据库中存储选定的CheckedListBox值

时间:2015-07-27 05:38:27

标签: c# checkedlistbox

我的数据库中有两个表。让我们说表A和表B.表A值放在checkedlistbox中。然后将checkedlistbox中的选定值放入表B.我尝试制作代码,但它不起作用。您对如何解决此问题有任何想法吗?

先谢谢你们。

顺便说一下我用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 Form8 : Form
    {
        public Form8()
        {
            InitializeComponent();
            this.Load += Form8_Load;
            button2.Click += button2_Click;
        }

        private void Form8_Load(object sender, EventArgs e)
        {
            string connstring = ("Server=localhost;Port=5432;User Id=postgres;Password=021393;Database=postgres;");
            NpgsqlConnection conn = new NpgsqlConnection(connstring);
            NpgsqlCommand cmd = new NpgsqlCommand("SELECT conname FROM condition", conn);
            cmd.CommandType = CommandType.Text;
            conn.Open();
            using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd))
            {
                DataTable dt = new DataTable();
                da.Fill(dt);
                ((ListBox)checkedListBox1).DataSource = dt;
                ((ListBox)checkedListBox1).DisplayMember = "conname";
                ((ListBox)checkedListBox1).DisplayMember = "conid";
                string[] condition = dt.Rows[0]["conname"].ToString().Split(',');
            }

        }
        private void button2_Click(object sender, EventArgs e)
        {
            string connstring = ("Server=localhost;Port=5432;User Id=postgres;Password=021393;Database=postgres;");
            NpgsqlConnection conn = new NpgsqlConnection(connstring);
            NpgsqlCommand cmd = new NpgsqlCommand("Insert into famhistory(famid) Values (@famid)", conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@famid", checkedListBox1.Text);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Data has been saved");
            conn.Close();

        }

    }
}

1 个答案:

答案 0 :(得分:0)

您必须遍历所有选定的项目:

//check if any item is selected
if (checkedListBox1.SelectedItems.Count > 0)
{
    //connect to database
    string connstring = ("Server=localhost;Port=5432;User Id=postgres;Password=021393;Database=postgres;");
    NpgsqlConnection conn = new NpgsqlConnection(connstring);
    conn.Open();

    //loop through all selected items
    foreach (object item in checkedListBox1.CheckedItems)
    {
        //convert item to string
        string checkedItem = item.ToString();

        //insert item to database
        NpgsqlCommand cmd = new NpgsqlCommand("Insert into famhistory(famid) Values (@famid)", conn);
        cmd.Parameters.AddWithValue("@famid", checkedItem); //add item
        cmd.ExecuteNonQuery();
    }

    //close connection
    conn.Close();
    MessageBox.Show("Data has been saved");
}

注意:我在一个打开的连接中执行所有插入命令,因为经常打开和关闭连接不是最佳做法。