将周添加到日期vb.net

时间:2021-12-23 20:52:33

I'm currently making an event program in VB.NET where the user can add daily/weekly/monthly/yearly events to a database. Their are two options : -Adding the occurrence x times to a StartDate. (http://i.imgur.com/LiRrZYy.png[^]) -Adding the occurrence between a StartDate and an EndDate. (http://i.imgur.com/gC6bcSt.png[^]) When i'm adding the weeks from 2 april until 29 of may / 2 april x times it generates some random values in it. Is there any way of fixing this ? This is my code :

我目前正在VB.NET中制作一个事件程序,用户可以将每日/每周/每月/每年的事件添加到数据库中。它们有两个选项: - 将出现次数添加到StartDate。 (http://i.imgur.com/LiRrZYy.png[^]) - 在StartDate和EndDate之间添加事件。 (http://i.imgur.com/gC6bcSt.png[^])当我在4月2日至5月29日加入周数时间时,它会产生一些随机值。有没有办法解决这个问题?这是我的代码:

Case rbnWeekly.Checked 'Weekly mode with Enddate and StartDate
                If rbnStartEnd.Checked = True Then
                    Dim startDate As Date = dtpStartDate.Text
                    Dim endDate As Date = dtpEndDate.Text
                    Dim currDate As Date = startDate
                    Do While (currDate < endDate)
                        SQL = String.Format("INSERT INTO tblManual (StartDate, EndDate, UniqueID) VALUES (#{0}#, #{1}#, '{2}')", currDate, endDate, id)
                        scmdRecurrence.CommandText = SQL
                        Valueee += 1
                        scmdRecurrence.ExecuteNonQuery()
                        currDate = currDate.AddDays(7)
                    Loop
                    MsgBox(Valueee.ToString & " records toegevoegd")
                Else 'Weekly mode with x times
                    Dim aantal As Integer = txtHerhaling.Text
                    Dim startDate As Date = Date.Today
                    Dim endDate As Date = startDate.AddDays(aantal * 7)
                    Dim currDate As Date = startDate
                    Do While (currDate < endDate)
                        SQL = String.Format("INSERT INTO tblManual (StartDate, EndDate, UniqueID) VALUES (#{0}#, #{1}#, '{2}')", currDate, endDate, id)
                        scmdRecurrence.CommandText = SQL
                        Valueee += 1
                        scmdRecurrence.ExecuteNonQuery()
                        currDate = currDate.AddDays(7)
                    Loop
                    MsgBox(Valueee.ToString & " records toegevoegd")
                End If

1 个解决方案

#1


In the SQL, you need properly formatted string expressions for the date values to be inserted:

在SQL中,您需要为要插入的日期值使用格式正确的字符串表达式:

.. , currDate.ToString("yyyy'/'MM'/'dd"), endDate.ToString("yyyy'/'MM'/'dd"), id)

#1


In the SQL, you need properly formatted string expressions for the date values to be inserted:

在SQL中,您需要为要插入的日期值使用格式正确的字符串表达式:

.. , currDate.ToString("yyyy'/'MM'/'dd"), endDate.ToString("yyyy'/'MM'/'dd"), id)