【问题标题】:store all selected values from checkedlistbox to database将选中列表框中的所有选定值存储到数据库
【发布时间】:2015-08-04 01:48:56
【问题描述】:

我正在用 c# 制作一个 windows 窗体,其中已选中列表框中的所有值都将存储到数据库中。但是代码实际上可以工作:

  1. 只有最后检查的值会像下面的数据一样重复存储在数据库中
  2. famhistory 表上的员工 ID (id) 不会显示为外键,但它会像下面的数据一样出现在表 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(',');

        }
    }
}

【问题讨论】:

  • 你创建了一个值变量并且从不使用它。
  • @dbugger 抱歉,我不明白你的意思
  • 这可能是您要保存到数据库的内容... string value = view.Row[0].ToString();但你永远不会用它做任何事情。
  • 是的。但我不知道如何实际使用它。能给个主意吗?

标签: c# npgsql checkedlistbox


【解决方案1】:

将插入操作代码更改为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,这只会让您获得最后选择的值。

【讨论】:

  • 我正在使用 DataRowView,因为如果我不使用它,只有 System.Data.DataRowView 会出现在我的表格中
  • 不,你当然不需要使用它。
  • 我现在尝试使用您的代码,但只有 System.Data.DataRowView 会出现在我的数据库表中,而不是 checklistvalue。
猜你喜欢
  • 2019-10-29
  • 1970-01-01
  • 1970-01-01
  • 2015-01-31
  • 1970-01-01
  • 2015-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多