将多个选定的列表框项目插入到 SQL 表中的同一个单元格中

我想将多个列表框项目插入到 SQL 表中的一个单元格中,并用逗号分隔项目。下面发布的代码只会添加列表框中的第一个选定项目。因此,如果您选择 2 或 10 个项目,您选择的第一个项目将被插入到表格中。for 循环是我的问题,我需要获取所有选定的值。谢谢


        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 = "";


                }

            }

        }

    }


慕容3067478
浏览 199回答 2
2回答

杨魅力

添加以下命名空间:using&nbsp;System.Linq;创建所选项目的字符串数组,然后使用 string.join:&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;selection&nbsp;=&nbsp;listbox_software.SelectedItems &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Cast<string>() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToArray(); &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;str&nbsp;=&nbsp;string.Join(",",&nbsp;selection);

慕少森

我找到了答案。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // To access checkbox list item's value //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string total = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (ListItem listItem in listbox_software.Items)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (listItem.Selected)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total =&nbsp; total + "[" + listItem.Value + "][ " + " ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string str = total.ToString();
打开App,查看更多内容
随时随地看视频慕课网APP