我想把checkedListBox选中的项放在一个数组中,再利用这个数组将选中的项插入数据库(checkedListBox选中几项数据库就插几条记录)该怎么写,谢谢。

解决方案 »

  1.   

    string[] arr = new string[CheckBoxList1.Items.Count];
            int index = 0;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    arr[index] = CheckBoxList1.Items[i].Value;
                    index++;
                }
            }然后将arr中不为空的数据循环插入数据库
      

  2.   

    把选中的项追加起来,格式如1,2,3,...
    操作数据库用如下
    create proc protest
    @tmp varchar(500)
    as 
    declare @u_Index int,@u_New varchar(20)
    while(charindex(',',@tmp)<>0)
    begin 
       set @u_Index = charindex(',',@tmp )
       set @u_New = left(@tmp ,@u_Index-1)
       set @tmp = substring(@tmp ,@u_Index+1,len(@tmp ))
       insert into ........@u_New
    end
      

  3.   

    插数据库可不可以给我一个SQL语句,不想用存储过程。
      

  4.   

    还用上面的方法,不过都写在程序中用循环控制,都次写一个SQL语句,保存到数据库。
    insert into table(field) values()
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
      

  5.   

    我的做法是用一个checkboxlist cbl,有6个item.
    StringBuilder abc= new StringBuilder();
     for(int i = 0;i < this.cbl.Items.Count;i++)
     {
        if(this.cbl.Items[i].Selected == true)
        abc.Append("1");
        else
        abc.Append("0");
     }那么把abc存到数据库里面(abc就是一串"1010"的数字选中的为1没选的为0),要看哪些被选中了,再把上面的过程反过来执行一遍就行了.