如何生成从checkedlistbox检查的每个项目的范围(从开始日期到结束日期)并插入表格? [英] How to generate each item(s) checked from checkedlistbox with ranges (from start date to end date) and insert into table?

查看:95
本文介绍了如何生成从checkedlistbox检查的每个项目的范围(从开始日期到结束日期)并插入表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前代码仅在checkedlistbox中插入一个已检查项目加上(开始日期到结束日期)的范围,尽管我检查了多个项目。我希望它是如果我检查5项,因此我的表内应包含5个选中的项目加上日期范围



例如:



donald | 01.01.14

donald | 01.02.14

donald | 01.03.14



helloword | 01.01.14

helloword | 01.02.14

helloword | 01.03.14



Wong | 01.01.14

Wong | 01.02.14

Wong | 01.03.14





代码:

current code only insert one checked item from checkedlistbox plus ranges of (start date to end date) although i had checked multiples item(s). i want it to be if i checked 5 items therefore inside my table should contain 5 checked item(s) plus the ranges of dates

e.g.:

donald|01.01.14
donald|01.02.14
donald|01.03.14

helloword|01.01.14
helloword|01.02.14
helloword|01.03.14

Wong|01.01.14
Wong|01.02.14
Wong|01.03.14


Codes:

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
    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", itemchecked.ToString)
        cmd.Parameters.AddWithValue("@Dateplease", stard)
        If connection.State = ConnectionState.Closed Then
            connection.Open()
        End If
        cmd.ExecuteNonQuery()
        stard = stard.AddDays(1)
    Loop
Next
connection.Close()

推荐答案

从哪里调用此代码?你调试过吗?它是否找到了您检查过的所有项目(换句话说,它是否在For Each循环中循环多次)?如果是这样,请浏览它并观察stard和endd变量发生的情况。它们似乎没有被重置每个被检查的项目,所以我猜测第二个被检查的项目已经有一个stard< = endd并且没有做任何事情,依此类推。
Where is this code being called from? Have you debugged? Is it finding all the items that you have checked (in other words, is it looping more than once in the For Each loop)? If so, walk through it and watch what is happening with the stard and endd variables. They don't appear to ever get reset per checked item, so I'd guess that the second checked item already has a stard<=endd and doesn't do any thing, and so on.


Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & aaa & "';Persist Security Info=False;")
        Dim originalstard As DateTime = DateTimePicker1.Value
        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(i)
            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
            stard = originalstard
            'Next
        Next
        connection.Close()


这篇关于如何生成从checkedlistbox检查的每个项目的范围(从开始日期到结束日期)并插入表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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