添加多个列表框项目数据库 [英] Adding multiple listbox items to database

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

问题描述

有没有简单的方法来在列表框中的项目保存到数据库中。我使用access数据库为Windows
组成,其中用户从下拉框选择项目,并将其添加到列表框中。



现在我想在列表框中的所有项目添加到与逗号分隔的数据库。
我如何才能执行此?



下面是类代码



 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Threading.Tasks
;使用System.Windows.Forms的
;
使用System.Data.OleDb;

命名空间Purchase_Management
{
公共部分Form1类:表格
{

串CONNSTRING =供应商= Microsoft.ACE.OLEDB。 12.0;数据源= C:\\Users\\Amrit\\Desktop\\Database.accdb;坚持安全信息= FALSE;;
公共Form1中()
{
的InitializeComponent();
}

私人无效Form1_Load的(对象发件人,EventArgs五)
{
comboBox1.SelectedText =先生;
comboBox1.Items.Add(先生);
comboBox1.Items.Add(夫人);
comboBox1.Items.Add(小姐);
的DataSet DS = GetAllItems();
comboBox2.DataSource = ds.Tables [0];
comboBox2.DisplayMember =产品名称;


}

公共数据集GetAllItems()
{
DataSet中的DataSet =新的DataSet();
//创建一个连接对象
OleDbConnection的oleConn =新的OleDbConnection(CONNSTRING);

{
oleConn.Open();
字符串SQL =SELECT [产品名称] FROM [产品];
OleDbDataAdapter的DataAdapter的=新OleDbDataAdapter的(SQL,oleConn);
DataAdapter.Fill方法(DataSet中,产品);
}
赶上(异常前)
{
Console.WriteLine(ex.ToString());
}
终于
{
oleConn.Close();
}
如果(dataSet.Tables.Count< = 0)
返回NULL;
,否则
返回数据集;
}


公共字符串InsertUser(字符串CUSTNAME,串题,串卡斯特,字符串phoneNumber的,字符串地址1,字符串地址2,字符串市,字符串邮编,串乡村,串itemPurchased )
{

//创建一个连接对象
INT九= 0;
串rTurn =;
OleDbConnection的oleConn =新的OleDbConnection(CONNSTRING);

{
oleConn.Open();
字符串SQL =INSERT INTO [客户]([客户Ebayname],[标题],[客户名称],[电话号码],[地址1],[地址2],[市],[邮编],[国家],[购买项目])+
VALUES(@custName,@title,@cust,@phoneNumber,@地址1,@地址2,@city,@postCode,@country,@itemPurchased)
OleDbCommand的oleComm =新的OleDbCommand(SQL,oleConn);

oleComm.Parameters.Add(@ CUSTNAME,OleDbType.Char).value的= CUSTNAME;
oleComm.Parameters.Add(@题,OleDbType.Char).value的=称号;
oleComm.Parameters.Add(@卡斯特,OleDbType.Char).value的=卡斯特;
oleComm.Parameters.Add(@ phoneNumber的OleDbType.Char).value的= phoneNumber的;
oleComm.Parameters.Add(@地址1,OleDbType.Char).value的=地址1;
oleComm.Parameters.Add(@地址2,OleDbType.Char).value的= 1地址;
oleComm.Parameters.Add(@城,OleDbType.Char).value的=城市;
oleComm.Parameters.Add(@邮编,OleDbType.Char).value的=邮编;
oleComm.Parameters.Add(@国,OleDbType.Char).value的=国家;
oleComm.Parameters.Add(@ itemPurchased,OleDbType.Char).value的= itemPurchased;


九= oleComm.ExecuteNonQuery();
如果(九大于0)
rTurn =用户添加;
,否则
rTurn =插入失败;
}
赶上(异常前)
{
Console.WriteLine(ex.ToString());
rTurn = ex.ToString();
}
终于
{
oleConn.Close();
}
返回rTurn;
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
InsertUser(textBox1.Text,comboBox1.Text,textBox2.Text,textBox3.Text, textBox4.Text,textBox5.Text,textBox6.Text,textBox7.Text,textBox8.Text,comboBox2.Text);
如果(MessageBox.Show(客户详细信息保存Successfuly)== DialogResult.OK)
{
Form1.ActiveFor​​m.Close();


}
}

私人无效button2_Click(对象发件人,EventArgs五)
{
listBox1.Items.Add (comboBox2.Text);
}

私人无效button3_Click(对象发件人,EventArgs五)
{
如果(this.listBox1.SelectedIndex> = 0)
本。 listBox1.Items.RemoveAt(this.listBox1.SelectedIndex);
}



}
}


< DIV CLASS =h2_lin>解决方案

第1步



并置在ListBox中的所有项目。 的string.join 需要字符串数组值,并返回一个字符串,将其连接在一起。请考虑使用 ListBox.Items财产它包含了所有你已经添加的项目。



第2步



插入在任何数据库您需要的字符串。写作如果你重用你的产品表中的itemPurchased一栏,你就可以使用你从上面的步骤1串联字符串。



短为你的整个代码,我不知道还有什么我们能为你在这里做。


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?

Here is the code for the class

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



    }
}

解决方案

Step 1

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.

Step 2

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆