使用C#在数据库中的单个列中插入多个复选框列表值 [英] to insert multiple checkbox list values into database in single column using c#
本文介绍了使用C#在数据库中的单个列中插入多个复选框列表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在我的SQL数据库的单列中插入多个复选框列表值.由于插入语句,仅插入一个值.
我想要插入多个值. For循环不起作用.
任何帮助将不胜感激.
I am trying to insert multiple checkbox list values in my SQL database in single column. Only one value is inserting because of insert statement.
I want multiple values to be inserted. For-loop is not working.
Any help would be appreciated.
for (int i = 0; i < CheckBoxList1.Items.Count - 1; i++)
{
String str = "";
if (CheckBoxList1.Items[i].Selected)
{
str = CheckBoxList1.Items[i].Text;
con.Open();
string sql = "Insert into LibManAddBook(Category,BookTitle,Feature,SubCategory)values('" + DDLCategory.SelectedItem.Text + "','" + TxtBooktitle.Text + "','" + CheckBoxList1.Text + "','" + DDLSubcategory.SelectedItem.Text + "')";
SqlCommand cmd = new SqlCommand(sql, con);
....
}
}
推荐答案
如果您已经调试过,那么您会发现执行确实输入了For-Loop.您的插入查询不正确.
您需要使用: CheckBoxList1.Items [i] .Text
试试:
If you would had debugged then you would had found that execution does enter in For-Loop. Your Insert query is not correct.
You need to use: CheckBoxList1.Items[i].Text
Try:
string sql = "Insert into LibManAddBook(Category,BookTitle,Feature,SubCategory)values(''" + DDLCategory.SelectedItem.Text + "'',''" + TxtBooktitle.Text + "'',''" + CheckBoxList1.Items[i].Text + "'',''" + DDLSubcategory.SelectedItem.Text + "'')";
只需使用DEBUGGER并查看其工作状况,您就应该能够轻松解决此类问题.
Just use DEBUGGER and see how things are working and you should be able to resolve such issues easily.
我做的与您所说的相同. > 任何帮助将不胜感激.
I did the same what u have said..but its not working..
Any help would be appreciated.
这篇关于使用C#在数据库中的单个列中插入多个复选框列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文