将日期添加到日期而不生成随机值。 [英] Add a week to a date without generating random values.

查看:86
本文介绍了将日期添加到日期而不生成随机值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在VB.NET中制作一个事件程序,用户可以将每日/每周/每月/每年的事件添加到数据库。

他们有两种选择:

-将事件x次添加到StartDate。

http://i.imgur。 com / LiRrZYy.png [ ^ ])

- 在StartDate和EndDate之间添加事件。

http:// i.imgur.com/gC6bcSt.png [ ^ ])

当我将4月2日至5月29日的周数加上x / 4时,它会产生一些随机值。有没有办法解决这个问题?

这是我的代码:



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 :

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



亲切问候!


Kind regards !

推荐答案

如comm所述你的代码容易受到SQL注入的攻击,建议你使用参数。



至于你的问题,我认为你需要稍微调整一下代码:



As mentioned in the comments, your code is vulnerable to SQL injections, recommend you use parameters.

As for your problem, I think you need to adjust the code just slightly:

Else 'Weekly mode with x times
     Dim aantal As Integer = txtHerhaling.Text
     Dim gebruik As Integer = 0
     Dim startDate As Date = Date.Today
     Dim endDate As Date = dtpEndDate.Text
     Dim currDate As Date = startDate
     Do While (currDate < endDate AndAlso gebruik < aantal)
         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)
         gebruik += 1
     Loop
     MsgBox(Valueee.ToString & " records toegevoegd")
 End If


看一下图片,我们可以很容易地看到问题与订单MM / DD / YY与DD / MM / YY有关。看起来在两种情况下只要日期有效就会使用错误的格式。



顺便说一下,如果yoy会遵循其他人给出的建议使用参数,这可能会解决问题。



图像中显示的表是否由此代码写入数据库,并由允许查看SQL表的程序显示。因为否则,如果是显示数据的代码,那么问题也可能是加载时间。



数据库中列的类型是什么?



还要检查为您的数据库,计算机和应用程序设置的文化。



您可能必须明确设置正确的文化或使用特定格式。



更新

如果多个线程是解析或格式化日期,你还需要确保在所有情况下都使用相同的文化。



顺便说一下,你可能在数据库中使用了一个订单使用另一个和一些代码尝试猜测格式,它是无效的尝试别的。
Looking at the picture, we can easily see that the problem is related to the order MM/DD/YY vs DD/MM/YY. It look like the bad format is used whenever a date is valid in both cases.

By the way, if yoy would follows the recommendation given by other to use parameters, it might fix the problem.

Does the tables shown in images are written in the database by this code and displayed by a program that allows to view SQL table. Because otherwise, if it is your code that display the data, then the problem could also be a load time.

What is the type of your columns in the database?

Also check which culture is set for your database, for your computer and for the application.

You might have to explicitly set the proper culture or use specific format.

Update
If multiple threads are parsing or formatting dates, you also need to be sure that the same culture is used in all cases.

By the way, you are probably using one order while the database use another one and some code try to guess the format and it it is not valid try something else.


这篇关于将日期添加到日期而不生成随机值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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