In this post, we’ll learn How to insert multiple data from Listbo into Sql Table.
Sql Database >> dbCce
Table >> students

Form Design:

Add Button:
1 2 3 4 5 6 7 | private void btnAdd_Click(object sender, EventArgs e) { listBox1.Items.Add(txtStudent.Text); txtStudent.Text = ""; } |
Save Button:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | private void btnAktar_Click(object sender, EventArgs e) { using (SqlConnection con = new SqlConnection("server =.; Initial Catalog = dbCce; Integrated Security = SSPI")) { con.Open(); using (SqlCommand cmd = new SqlCommand("Insert Into students (name) VALUES (@sname)", con)) { foreach (var item in listBox1.Items) { cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@sname", item.ToString()); cmd.ExecuteNonQuery(); } } } } |