如何将checkedlistbox中的已检查项目加上表格的时间范围?

时间:2014-06-23 04:55:08

标签: vb.net vb.net-2010

以下代码仅循环checklistbox中的第一个已检查项目,我希望它能够循环每个已检查项目以及开始日期和结束日期的范围以及插入到表格之后。表中的结果将是:

user  |  01.01.14
user  |  01.02.14
user  |  01.03.14
user  |  01.04.14

user2  |  02.01.14
user2  |  02.02.14
user2  |  02.03.14
user2  |  02.04.14

但是下面的代码只生成第一个检查项目的结果,虽然我确实检查了多个项目。

首选结果不正确,谁可以帮助或指导我?

    Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & aaa & "';Persist Security Info=False;")
    Dim stard As DateTime = DateTimePicker1.Value
    Dim endd As DateTime = DateTimePicker2.Value

    'Dim itemchecked As Object
    'For Each itemchecked In CheckedListBox1.CheckedItems
    Dim count = CheckedListBox1.CheckedItems.Count
    'CheckedListBox1.Items.Clear()
    For i As Integer = 0 To (count - 1)
        Dim checked = CheckedListBox1.CheckedItems.Item(0)
        Do While stard <= endd
            Dim insertinto As String = "INSERT INTO try ([SN],[Dateplease]) VALUES (@SN, @Dateplease)"
            Dim cmd As New OleDbCommand(insertinto, connection)
            cmd.Parameters.AddWithValue("@SN", checked)
            cmd.Parameters.AddWithValue("@Dateplease", stard)
            If connection.State = ConnectionState.Closed Then
                connection.Open()
            End If
            cmd.ExecuteNonQuery()
            stard = stard.AddDays(1)
        Loop
        'Next
    Next
    connection.Close()

1 个答案:

答案 0 :(得分:2)

Dim checked = CheckedListBox1.CheckedItems.Item(i)

您需要将开始日期值重置为原始值
添加此

Dim originalstard As DateTime = DateTimePicker1.Value
Dim stard As DateTime = DateTimePicker1.Value
Dim endd As DateTime = DateTimePicker2.Value

然后在Loop关键字后添加

Loop
stard = originalstard