添加列表框的所有项目以访问数据库

时间:2013-02-15 11:03:11

标签: c# winforms listbox

如何将列表框中的所有项目添加到数据库中。

当我选择所有项目并使用以下代码时,它会将第一项添加到数据库中。

enter image description here

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, listBox1.SelectedItem.ToString());
            SaveAllListItems();
            if (MessageBox.Show("Customer Details Saved Successfuly") == DialogResult.OK)
            {
                Form1.ActiveForm.Close();


            }
        }

        private void SaveAllListItems()
        {
            foreach (var listBoxItem in listBox1.Items)
            {
                InsertUser(textBox1.Text, comboBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text,
                    textBox5.Text, textBox6.Text, textBox7.Text, textBox8.Text, listBoxItem.ToString());
            }
        }

        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);
        }



    }
}

如何将所有项目添加到数据库?

还有什么办法以编程方式选择所有项目&gt;

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

 private void SaveAllListItems()
        {
            foreach (var listBoxItem in listBox1.Items)
            {
                InsertUser(textBox1.Text, comboBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, 
                    textBox5.Text, textBox6.Text, textBox7.Text, textBox8.Text, listBoxItem.ToString());
            }
        }

要选择所有项目,请使用此选项:

  listBox1.SelectionMode = SelectionMode.MultiSimple;

   for (int i = 0; i < listBox1.Items.Count;i++)
   {
       listBox1.SetSelected(i, true);
   }

<强>更新

试试这个

        private void SaveAllListItems()
        {
            string listItems = string.Empty;
            foreach (var listBoxItem in listBox1.Items)
            {

                listItems += listBoxItem.ToString();

                if (listBox1.Items.IndexOf(listBoxItem) < listBox1.Items.Count - 1)
                {
                    listItems += ", ";
                }
            }

            InsertUser(textBox1.Text, comboBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text,
                       textBox6.Text, textBox7.Text, textBox8.Text, listItems);
        }