从列表框将项目列表保存到数据库 [英] Saving a list of items to a database from a listbox
本文介绍了从列表框将项目列表保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿!在将项目列表从列表框保存到数据库时,我一直遇到问题.请告诉我如何通过单击一次保存按钮将所有项目保存到表中.
Hey! I keep having problems with saving a list of items from my listbox to the database. Please show me how i would save all items to a table by clicking the save button once.
推荐答案
将所有这些记录添加到datatable中,然后将datatable插入数据库中
希望此链接能解决您的问题.
http://msdn.microsoft.com/en-us/library/33y2221y%28v = vs.71%29.aspx [ ^ ]
Add all these records in datatable then insert datatable in database
Hope this link will solve your problem.
http://msdn.microsoft.com/en-us/library/33y2221y%28v=vs.71%29.aspx[^]
尝试一下:
Try this:
public string GetListItems()
{
System.Collections.Generic.List<string> items = new System.Collections.Generic.List<string>();
foreach (ListItem item in userList.Items)
{
if (item.Selected)
{
items.Add(item.Text);
}
}
return String.Join(", ", items.ToArray());
}
希望对您有所帮助:)
hope it helps :)
假设您在列表框中有项目.
添加保存按钮,并在click事件中进行如下操作:
suppose you have items in a list box.
Add save button and in the click event as follows:
for (int i = 0; i < ListBox1.Items.Count; i++)
{
ListItem item= new ListItem();
item.Text = ListBox1.Items[i].Text;
item.Value = ListBox1.Items[i].Value;
SqlCommand cmd=new SqlCommand("insert into tablename(listcolmText,listcolmValue) values('"+item.Text +"',"+ item.Value +")",conn);
cmd.ExecuteNonQuery();
}
这篇关于从列表框将项目列表保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文