如何在sqlserver数据库中插入checkboxlist选中的值? [英] How to insert the checkboxlist selected values in sqlserver database?

查看:143
本文介绍了如何在sqlserver数据库中插入checkboxlist选中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子A:数据是

i have one table A :data's are

categoryid   categoryname     image
   1           Breakfast   <Binarydata>
   2           Lunch       <Binarydata>
   3           Dinner      <Binarydata>



在PageLoad中我将这些数据显示在Checkboxlist中


In PageLoad i display these data's in Checkboxlist

SqlCommand cmd1 = new SqlCommand("select Categoryname from A", connect);
                SqlDataReader dr1 = cmd1.ExecuteReader();
                while (dr1.Read())
                {
                    chkboxcategory.Items.Add(dr1.GetValue(0).ToString());
                }



在早餐和晚餐中添加像咖啡这样的项目意味着在复选框列表中选择两个值然后我添加另一个表B..Example


after add some item like Coffee in both breakfast and dinner means select two value in checkbox list then i add another table B..Example

id    name   categoryname
1     Coffee   Breakfast
2     Coffee   Dinner



如何在B表中插入数据?


How to insert the data's in B table?

推荐答案

尝试这...... :)



Try this...:)

foreach (ListBox lb in checkedListBox1.Items)
{
    if (lb.SelectedItem)
    {
        str += lb.ValueMember + ",";
    }
}


代码:



Code:

for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected == true)
            {
                str += " " + CheckBoxList1.Items[i].ToString();
            }
        }







其中str可以是数组或简单字符串,根据您的要求...

希望这符合您的要求..

谢谢。




where str can be an array or a simple string, as per your requirement...
Hope this meets your requirement..
Thanks.






for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected == true)
            {
                string sqlInsert = "insert into tableB values (3,'Coffee','"+CheckBoxList1.Items[i].ToString()+"')";
SqlCommand cmd1 = new SqlCommand(sqlInsert ,connect);
cmd1.executenonquery();
            }
        }


这篇关于如何在sqlserver数据库中插入checkboxlist选中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆