插入列表框的项目到SQL Server数据库 [英] inserting items of listbox into sql server database
本文介绍了插入列表框的项目到SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用此code在插入数据ListBox1的:
i am inserting data in listbox1 by using this code :
if (!IsPostBack)
{
DataTable dt = new DataTable();
string query = "SELECT * FROM [Qualification]";
SqlDataAdapter objda = new SqlDataAdapter(query, conn);
//conn.Open();
objda.Fill(dt);
if (dt.Rows.Count > 0)
{
ListBox1.DataSource = dt;
ListBox1.DataTextField = "QDesc";
ListBox1.DataValueField = "QID";
ListBox1.DataBind();
}
}
和这些项目进入 listbox2
按钮单击
and these items are going into listbox2
on button click
按钮点击code是:
protected void Button1_Click1(object sender, EventArgs e)
{
string strItemText = "";
string strItemValue = "";
foreach (ListItem listItem in ListBox1.Items)
{
if (listItem.Selected == true)
{
strItemValue += listItem.Text;
}
}
ListBox2.Items.Add(strItemText+""+ strItemValue);
ListBox1.Items.Remove(ListBox1.SelectedItem);
}
一切都是正确的,直到这里..
everything is proper till here..
我想在 listbox2
的项目应在数据库表中填充....谁能帮助建议code为同一
i want the items in listbox2
should be populated in database table....can anyone help to suggest code for same
推荐答案
当然,你可以做这样的事情:
Sure, you could do something like this:
using (SqlConnection c = new SqlConnection("your connection string"))
{
c.Open();
using (SqlCommand cmd = new SqlCommand("INSERT INTO table (field1, field2) VALUES (@field1, @field2)")
{
foreach (ListItem item in listbox2.Items)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@field1", item.Value);
cmd.Parameters.AddWithValue("@field2", item.Text);
cmd.ExecuteNonQuery();
}
}
}
现在显然我提供的是不同的插入
语句,并添加会有所不同的参数,也许你并不需要在值
和文本
,但你的想法。
Now clearly the INSERT
statement I provided would be different, and the parameters you add would be different, and maybe you don't need the Value
and the Text
, but you get the idea.
这篇关于插入列表框的项目到SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文