【发布时间】:2014-02-25 00:02:54
【问题描述】:
我有 5 个复选框列表。每个复选框里面有 6 个复选框(下面是我的数据库)
checkboxlist1 : 行
checkboxlist2:rowb
checkboxlist3:rowc
checkboxlist4:rowd
checkboxlist5:rowe
id:整数, 罗瓦:位, 行:位, 行:位, 行:位, 罗:位,
rowtext:nvarchar(50)
我的问题是,将项目从一个 CheckBoxList 添加到另一个 CheckBoxList 时
,如何检查item是否已经存在于第二、第三、第四、第五个CheckBoxLists中?
这是我的代码
Protect sub_button1
'一个复选框列表 对于 As Integer = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(a).Selected Then
rowtext = CheckBoxList1.Items(a).Text
rowa = 1
rowb = 0
rowc= 0
rowd= 0
Dim insertSql As String = "INSERT INTO tbtest(rowtext,rowa,rowb,rowc,rowd,) VALUES(@rowtext,@rowa,@rowb,@rowc,@rowd)"
Using myConnection As New SqlConnection(connectionString)
myConnection.Open()
Dim myCommand As New SqlCommand(insertSql, myConnection)
myCommand.Parameters.AddWithValue("@rowtext", SqlDbType.Bit).Value = rowtext
myCommand.Parameters.AddWithValue("@rowa", SqlDbType.Bit).Value = rowa
myCommand.Parameters.AddWithValue("@rowb", SqlDbType.Bit).Value = rowb
myCommand.Parameters.AddWithValue("@rowc", SqlDbType.Bit).Value = rowc
myCommand.Parameters.AddWithValue("@rowd", SqlDbType.Bit).Value = rowd
myCommand.ExecuteNonQuery()
myConnection.Close()
End Using
End If
Next
'B CheckBoxList
For b As Integer = 0 To CheckBoxList2.Items.Count - 1
If CheckBoxList2.Items(b).Selected Then
rowtext = CheckBoxList2.Items(b).Text
rowa = 0
rowb = 1
rowc= 0
rowd= 0
Dim insertSql As String = "INSERT INTO tbtest(rowtext,rowa,rowb,rowc,rowd) VALUES(@rowtext,@rowa,@rowb,@rowc,@rowd)"
Using myConnection As New SqlConnection(connectionString)
myConnection.Open()
Dim myCommand As New SqlCommand(insertSql, myConnection)
myCommand.Parameters.AddWithValue("@rowtext", SqlDbType.Bit).Value = rowtext
myCommand.Parameters.AddWithValue("@rowa", SqlDbType.Bit).Value = rowa
myCommand.Parameters.AddWithValue("@rowb", SqlDbType.Bit).Value = rowb
myCommand.Parameters.AddWithValue("@rowc", SqlDbType.Bit).Value = rowc
myCommand.Parameters.AddWithValue("@rowd", SqlDbType.Bit).Value = rowd
myCommand.ExecuteNonQuery()
myConnection.Close()
End Using
End If
Next
'C CheckBoxList For c As Integer = 0 To CheckBoxList3.Items.Count - 1
If CheckBoxList3.Items(c).Selected Then
rowtext = CheckBoxList3.Items(c).Text
rowa = 0
rowb = 0
rowc= 1
rowd= 0
Dim insertSql As String = "INSERT INTO tbtest(rowtext,rowa,rowb,rowc,rowd) VALUES(@rowtext,@rowa,@rowb,@rowc,@rowd)"
Using myConnection As New SqlConnection(connectionString)
myConnection.Open()
Dim myCommand As New SqlCommand(insertSql, myConnection)
myCommand.Parameters.AddWithValue("@rowtext", SqlDbType.Bit).Value = rowtext
myCommand.Parameters.AddWithValue("@rowa", SqlDbType.Bit).Value = rowa
myCommand.Parameters.AddWithValue("@rowb", SqlDbType.Bit).Value = rowb
myCommand.Parameters.AddWithValue("@rowc", SqlDbType.Bit).Value = rowc
myCommand.Parameters.AddWithValue("@rowd", SqlDbType.Bit).Value = rowd
myCommand.ExecuteNonQuery()
myConnection.Close()
End Using
End If
Next
'D CheckBoxList For d As Integer = 0 To CheckBoxList4.Items.Count - 1
If CheckBoxList4.Items(d).Selected Then
rowtext = CheckBoxList4.Items(d).Text
rowa = 0
rowb = 0
rowc= 0
rowd= 1
Dim insertSql As String = "INSERT INTO tbtest(rowtext,rowa,rowb,rowc,rowd) VALUES(@rowtext,@rowa,@rowb,@rowc,@rowd)"
Using myConnection As New SqlConnection(connectionString)
myConnection.Open()
Dim myCommand As New SqlCommand(insertSql, myConnection)
myCommand.Parameters.AddWithValue("@rowtext", SqlDbType.Bit).Value = rowtext
myCommand.Parameters.AddWithValue("@rowa", SqlDbType.Bit).Value = rowa
myCommand.Parameters.AddWithValue("@rowb", SqlDbType.Bit).Value = rowb
myCommand.Parameters.AddWithValue("@rowc", SqlDbType.Bit).Value = rowc
myCommand.Parameters.AddWithValue("@rowd", SqlDbType.Bit).Value = rowd
myCommand.ExecuteNonQuery()
myConnection.Close()
End Using
End If
Next
Catch ex As Exception
End Try
End Sub
【问题讨论】: