如何在数据库中只添加有值的文本框

  • 本文关键字:文本 添加 数据库 c#
  • 更新时间 :
  • 英文 :


这是我在数据库中添加值时的代码,但我的问题是,正在显示,我只想添加具有值

的文本框的值
SqlConnection con = new SqlConnection(@"Data Source = MAAAYYK; Initial Catalog = dbFoodReservation; Integrated Security = True");
SqlCommand cm = new SqlCommand();
cm = new SqlCommand("Insert into tbl_Reservation(Food)Values(@Food)", con);
cm.Parameters.AddWithValue("@Food", txtboxfood1.Text + "," + txtboxfood2.Text + "," + txtboxfood3.Text + "," + txtboxfood4.Text);

有几种方法可以做到这一点,您可以尝试string.Join:

var foodValues = new List<string>()
{
txtboxfood1.Text, txtboxfood2.Text, txtboxfood3.Text , txtboxfood4.Text
}
var foodText = string.Join(",", foodValues.Where(s => !string.IsNullOrEmpty(s)));
...
cm.Parameters.AddWithValue("@Food", foodText);

最新更新