Добавление нескольких элементов списка в базу данных
Есть ли простой способ сохранить элементы из списка в базе данных. Я использую базу данных доступа для формы Windows, где пользователь выбирает элементы из выпадающего списка и добавляет их в список.
Теперь я хочу добавить все элементы списка в базу данных через запятую. Как я могу это сделать?
Вот код для класса
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)
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);
}
}
}