如何使用c#将多个复选框值插入到SQL数据库的同一列?这应该通过点击按钮来完成

本文关键字:一列 按钮 复选框 何使用 插入 数据库 SQL | 更新日期: 2023-09-27 18:17:50

private void btn_Add_Tourist_Click(对象发送方,EventArgs e)

//如何使用c#将多个复选框值插入到SQL数据库的同一列?这应该通过点击按钮来完成{

      SqlConnection AddTourist = new SqlConnection("Data Source=VDESHANE;Initial Catalog=GreenwayTravelsLankaDB;Integrated Security=True");
       {
           SqlCommand Insert_TouristData = new SqlCommand
                ("Insert into Tourist_Details (First_Name, Last_Name, Country, Email, Number_Of_Pax, Adults, Children, Date_Of_Arrival, Date_Of_Departure, Meal_Plan, Celebrating, Category, Requests) Values (@First_Name, @Last_Name, @Country, @Email, @Number_Of_Pax, @Adults, @Children, @Date_Of_Arrival, @Date_Of_Departure, @Meal_Plan, @Celebrating, @Category, @Requests)", AddTourist);

            Insert_TouristData.Parameters.AddWithValue("@First_Name", txt_First_Name.Text);
            Insert_TouristData.Parameters.AddWithValue("@Last_Name", txt_Last_Name.Text);
            Insert_TouristData.Parameters.AddWithValue("@Country", txt_Country.Text);
            Insert_TouristData.Parameters.AddWithValue("@Email", txt_Email.Text);
            Insert_TouristData.Parameters.AddWithValue("@Number_Of_Pax", txt_Pax.Text);
            Insert_TouristData.Parameters.AddWithValue("@Adults", txt_Adults.Text);
            Insert_TouristData.Parameters.AddWithValue("@Children", txt_Children.Text);
            Insert_TouristData.Parameters.AddWithValue("@Date_Of_Arrival", date_Arrival.Value);
            Insert_TouristData.Parameters.AddWithValue("@Date_Of_Departure", date_Departure.Value);

            Insert_TouristData.Parameters.AddWithValue("@Meal_Plan", Meal_Plan_list.Text);
            Insert_TouristData.Parameters.AddWithValue("@Celebrating", Celebrating_list.Text);

//所有其他函数工作正常…但是复选框的问题仍然存在//<-------------不能正常工作

           while (check_Cultural_Site.Checked)
            {
            Insert_TouristData.Parameters.AddWithValue("@Category", "Cultural Site,");
                while (check_Boat_Trips.Checked)
                {
                    Insert_TouristData.Parameters.AddWithValue("@Category", "Boat Trips,");
                }
                while (check_Cycling.Checked)
                {
                    Insert_TouristData.Parameters.AddWithValue("@Category", "Cycling,");
                }
                while (check_Trekking.Checked)
                {
                    Insert_TouristData.Parameters.AddWithValue("@Category", "Trekking,");
                }
                while (check_Nature.Checked)
                {
                    Insert_TouristData.Parameters.AddWithValue("@Category", "Nature,");
                }
                while (check_Bird_Watching.Checked)
                {
                    Insert_TouristData.Parameters.AddWithValue("@Category", "Bird Watching,");
                }
                while (check_Rafting.Checked)
                {
                    Insert_TouristData.Parameters.AddWithValue("@Category", "Rafting,");
                }
                while (check_Beach_Vacation.Checked)
                {
                    Insert_TouristData.Parameters.AddWithValue("@Category", "Beach Vacation,");
                }
                while (check_Wild_Life.Checked)
                {
                    Insert_TouristData.Parameters.AddWithValue("@Category", "Wild Life,");
                }
                while (check_Camping.Checked)
                {
                    Insert_TouristData.Parameters.AddWithValue("@Category", "Camping,");
                }
                while (check_Shopping.Checked)
                {
                    Insert_TouristData.Parameters.AddWithValue("@Category", "Shopping,");
                }
                while (check_Dolphin.Checked)
                {
                    Insert_TouristData.Parameters.AddWithValue("@Category", "Dolphin,");
                }
                while (check_Diving.Checked)
                {
                    Insert_TouristData.Parameters.AddWithValue("@Category", "Diving,");
                }
                while (check_Whale_Watching.Checked)
                {
                    Insert_TouristData.Parameters.AddWithValue("@Category", "Whale Watching,");
                }
           }
  //NOT WORKING CORRECTLY ---------------->

           Insert_TouristData.Parameters.AddWithValue("@Requests", txt_Requests.Text);
            AddTourist.Open();
            Insert_TouristData.ExecuteNonQuery();
            AddTourist.Close();
            MessageBox.Show("DATA SUCCESSFULLY ADDED");

        }
    }
}

如何使用c#将多个复选框值插入到SQL数据库的同一列?这应该通过点击按钮来完成

使用StringBuilder创建一个字符串,其中所有的兴趣被逗号分隔,然后将该字符串作为一个整体存储在列Requests

StringBuilder sb = new StringBuilder();
if(check_Cultural_Site.Checked)
   sb.Append("Cultural Site,");
if(check_Boat_Trips.Checked)
   sb.Append("Boat Trips,");
..... and so on ....
... and at the end of the list of if .....
// Remove the last comma
if(sb.Length > 0)
   sb.Length--;
// Just add the parameter one time and assign the value of the StringBuilder to it
Insert_TouristData.Parameters.AddWithValue("@Category", sb.ToString()); 

你应该检查,如果有人勾选所有列表不超过1000个字符允许的datatable字段,为了更好的可读性的方法,我建议移动这个长列表的if到一个单独的函数。

string interests = GetInterests();
Insert_TouristData.Parameters.AddWithValue("@Category", interests);