將多個(gè)選定的列表框項(xiàng)目插入到 SQL 表中的同一個(gè)單元格中
我想將多個(gè)列表框項(xiàng)目插入到 SQL 表中的一個(gè)單元格中,并用逗號分隔項(xiàng)目。下面發(fā)布的代碼只會添加列表框中的第一個(gè)選定項(xiàng)目。因此,如果您選擇 2 或 10 個(gè)項(xiàng)目,您選擇的第一個(gè)項(xiàng)目將被插入到表格中。for 循環(huán)是我的問題,我需要獲取所有選定的值。謝謝 protected void pg_upload_Click(object sender, EventArgs e) { using (SqlConnection mycon = new SqlConnection(connectionstring)) { using (SqlCommand mycmd = mycon.CreateCommand()) { if (textbox_make.Text == string.Empty || textbox_number.Text == string.Empty) { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('The Make/Model and Number must be Entered')", true); } else { string str = ""; for (int i=0; i<= listbox_software.Items.Count; i++) { str = listbox_software.SelectedItem.ToString(); } mycon.Open(); mycmd.CommandText = "INSERT INTO tbl_PG (Model, PGNumber, AssetNo, Area, Owner,IPAddress, SerialNo, OSVersion, Memory, Software) " + "Values ('" + textbox_make.Text + "' , '" + textbox_number.Text + "' , '" + textbox_asset.Text + "' , '" + drop_area.Text + "' , '" + drop_owner.Text + "' , '" + textbox_ip.Text + "' " + ", '" + textbox_serial.Text + "' , '" + textbox_os.Text + "' , '" + textbox_memory.Text + "' , '" + str + "')"; mycmd.ExecuteNonQuery(); PopulateGridView(); lblsuscessmessage.Text = "Selected Record Added"; lblerrormessage.Text = ""; } } } }
查看完整描述