1. <tfoot id='g56Eq'></tfoot>
      <bdo id='g56Eq'></bdo><ul id='g56Eq'></ul>
    <legend id='g56Eq'><style id='g56Eq'><dir id='g56Eq'><q id='g56Eq'></q></dir></style></legend>
    <i id='g56Eq'><tr id='g56Eq'><dt id='g56Eq'><q id='g56Eq'><span id='g56Eq'><b id='g56Eq'><form id='g56Eq'><ins id='g56Eq'></ins><ul id='g56Eq'></ul><sub id='g56Eq'></sub></form><legend id='g56Eq'></legend><bdo id='g56Eq'><pre id='g56Eq'><center id='g56Eq'></center></pre></bdo></b><th id='g56Eq'></th></span></q></dt></tr></i><div id='g56Eq'><tfoot id='g56Eq'></tfoot><dl id='g56Eq'><fieldset id='g56Eq'></fieldset></dl></div>

    <small id='g56Eq'></small><noframes id='g56Eq'>

    1. 将多个列表框项目添加到数据库

      时间:2023-10-07

          <tbody id='iH5cy'></tbody>
      • <tfoot id='iH5cy'></tfoot>
          <bdo id='iH5cy'></bdo><ul id='iH5cy'></ul>

          <small id='iH5cy'></small><noframes id='iH5cy'>

          1. <legend id='iH5cy'><style id='iH5cy'><dir id='iH5cy'><q id='iH5cy'></q></dir></style></legend>
              1. <i id='iH5cy'><tr id='iH5cy'><dt id='iH5cy'><q id='iH5cy'><span id='iH5cy'><b id='iH5cy'><form id='iH5cy'><ins id='iH5cy'></ins><ul id='iH5cy'></ul><sub id='iH5cy'></sub></form><legend id='iH5cy'></legend><bdo id='iH5cy'><pre id='iH5cy'><center id='iH5cy'></center></pre></bdo></b><th id='iH5cy'></th></span></q></dt></tr></i><div id='iH5cy'><tfoot id='iH5cy'></tfoot><dl id='iH5cy'><fieldset id='iH5cy'></fieldset></dl></div>

                本文介绍了将多个列表框项目添加到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                有什么简单的方法可以将列表框中的项目保存到数据库中.我正在使用 Windows 窗体的访问数据库,用户从组合框中选择项目并将其添加到列表框中.

                Is there any easy way to save the items in listbox to the database. I am using access database for windows form where user selects items from the combobox and adds it to the list box.

                现在我想将列表框中的所有项目添加到用逗号分隔的数据库中.我该怎么做?

                Now i want to add all the items in the listbox to the database separated with comma. How can i perform this?

                这是类的代码

                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 System.Data.OleDb;
                
                namespace Purchase_Management
                {
                    public partial class Form1 : Form
                    {
                
                        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Amrit\Desktop\Database.accdb ;Persist Security Info=False;";
                        public Form1()
                        {
                            InitializeComponent();
                        }
                
                        private void Form1_Load(object sender, EventArgs e)
                        {
                            comboBox1.SelectedText = "Mr";
                            comboBox1.Items.Add("Mr");
                            comboBox1.Items.Add("Mrs");
                            comboBox1.Items.Add("Miss");
                            DataSet ds = GetAllItems();
                            comboBox2.DataSource = ds.Tables[0];
                            comboBox2.DisplayMember = "Product Name";
                
                
                        }
                
                        public DataSet GetAllItems()
                        {
                            DataSet dataSet = new DataSet();
                            // Create connection object
                            OleDbConnection oleConn = new OleDbConnection(connString);
                            try
                            {
                                oleConn.Open();
                                string sql = "SELECT [Product Name] FROM [Product]";
                                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, oleConn);
                                dataAdapter.Fill(dataSet, "Product");
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.ToString());
                            }
                            finally
                            {
                                oleConn.Close();
                            }
                            if (dataSet.Tables.Count <= 0)
                                return null;
                            else
                                return dataSet;
                        }
                
                
                        public string InsertUser(string custName, string title, string cust, string phoneNumber, string address1, string address2, string city, string postCode, string country, string itemPurchased)
                        {
                
                            // Create connection object
                            int ix = 0;
                            string rTurn = "";
                            OleDbConnection oleConn = new OleDbConnection(connString);
                            try
                            {
                                oleConn.Open();
                                string sql = "INSERT INTO [Customer]([Customer's Ebayname], [Title],  [Customer's Name], [Phone Number], [Address 1], [Address 2], [City], [Post Code], [Country] , [Item Purchased])" +
                                         "VALUES ( @custName, @title, @cust, @phoneNumber, @address1, @address2, @city, @postCode, @country , @itemPurchased)";
                                OleDbCommand oleComm = new OleDbCommand(sql, oleConn);
                
                                oleComm.Parameters.Add("@custName", OleDbType.Char).Value = custName;
                                oleComm.Parameters.Add("@title", OleDbType.Char).Value = title;
                                oleComm.Parameters.Add("@cust", OleDbType.Char).Value = cust;
                                oleComm.Parameters.Add("@phoneNumber", OleDbType.Char).Value = phoneNumber;
                                oleComm.Parameters.Add("@address1", OleDbType.Char).Value = address1;
                                oleComm.Parameters.Add("@address2", OleDbType.Char).Value = address2;
                                oleComm.Parameters.Add("@city", OleDbType.Char).Value = city;
                                oleComm.Parameters.Add("@postCode", OleDbType.Char).Value = postCode;
                                oleComm.Parameters.Add("@country", OleDbType.Char).Value = country;
                                oleComm.Parameters.Add("@itemPurchased", OleDbType.Char).Value = itemPurchased;
                
                
                                ix = oleComm.ExecuteNonQuery();
                                if (ix > 0)
                                    rTurn = "User Added";
                                else
                                    rTurn = "Insert Failed";
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.ToString());
                                rTurn = ex.ToString();
                            }
                            finally
                            {
                                oleConn.Close();
                            }
                            return rTurn;
                        }
                
                        private void button1_Click(object sender, EventArgs e)
                        {
                            InsertUser(textBox1.Text, comboBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text, textBox8.Text, comboBox2.Text);
                            if (MessageBox.Show("Customer Details Saved Successfuly") == DialogResult.OK)
                            {
                                Form1.ActiveForm.Close();
                
                
                            }
                        }
                
                        private void button2_Click(object sender, EventArgs e)
                        {
                            listBox1.Items.Add(comboBox2.Text);
                        }
                
                        private void button3_Click(object sender, EventArgs e)
                        {
                            if (this.listBox1.SelectedIndex >= 0)
                                this.listBox1.Items.RemoveAt(this.listBox1.SelectedIndex);
                        }
                
                
                
                    }
                }
                

                推荐答案

                第一步

                连接 ListBox 中的所有项目.String.Join 接受一个字符串数组值,并返回将它们连接在一起的单个字符串.考虑使用 ListBox.Items 属性其中包含您添加的所有项目.

                Concatenate all the items in your ListBox. String.Join takes an array of string values, and returns a single String which concatenates them together. Consider using the ListBox.Items property which contains all the items you've added.

                第 2 步

                在您想要的任何数据库中插入字符串.如果您在 Product 表中重复使用itemPurchased"列,您将能够使用您在上面第 1 步中连接的字符串.

                Insert the string in whichever database you want. If you're reusing the "itemPurchased" column in your Product table you'll be able to use the string you've concatenated from Step 1 above.

                没有为你编写完整的代码,我不确定我们还能为你做些什么.

                Short of writing the entire code for you, I'm not sure what else we can do for you here.

                这篇关于将多个列表框项目添加到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:(c# + windows forms) 将项目添加到不同类的listBox 下一篇:如何在“绑定时间"获取 ListBox 中项目的 ListBoxItem

                相关文章

                <tfoot id='RKEPi'></tfoot>
                <legend id='RKEPi'><style id='RKEPi'><dir id='RKEPi'><q id='RKEPi'></q></dir></style></legend>
                1. <i id='RKEPi'><tr id='RKEPi'><dt id='RKEPi'><q id='RKEPi'><span id='RKEPi'><b id='RKEPi'><form id='RKEPi'><ins id='RKEPi'></ins><ul id='RKEPi'></ul><sub id='RKEPi'></sub></form><legend id='RKEPi'></legend><bdo id='RKEPi'><pre id='RKEPi'><center id='RKEPi'></center></pre></bdo></b><th id='RKEPi'></th></span></q></dt></tr></i><div id='RKEPi'><tfoot id='RKEPi'></tfoot><dl id='RKEPi'><fieldset id='RKEPi'></fieldset></dl></div>
                    <bdo id='RKEPi'></bdo><ul id='RKEPi'></ul>

                    <small id='RKEPi'></small><noframes id='RKEPi'>