vb.net中的Datetimepicker日期选择问题
问题描述:
嗨那里,
i我在DateTimePicker中使用dd / MM / yyyy(自定义)格式,有两个datetimepicker
当我选择日期01/11/2014到06/11/2014并搜索数据然后
当它执行它使用
DateTimePicker1.Value.Date =#11/1/2014#
DateTimePicker2.Value.Date =#11/6 / 2014#
访问数据库(列名为billdate,日期/时间数据类型)
谢谢。
hi there,
i am using dd/MM/yyyy (custom) format in DateTimePicker, have two datetimepicker
when i select date 01/11/2014 to 06/11/2014 and search data then
when it execute it use
DateTimePicker1.Value.Date = #11/1/2014#
DateTimePicker2.Value.Date = #11/6/2014#
access database (column name is billdate with Date/Time datatypes)
Thanks.
答
正如Sinisa Hajnal所说,自定义格式没有影响实际日期,只是它显示和输入的方式。
当你设置或检索Value属性时,它使用DateTime属性,你可以直接传递给它通过使用参数化查询并传递DateTime值而不进行转换来访问(使用或不使用Date属性,只删除时间信息)。这样就不会混淆系统之间的日期格式了。
顺便说一句:在用户强制使用日期格式被认为是不好的做法:你通常应该使用文化他们的机器的具体日期形式,否则他们也会感到困惑!
那我怎样才能搜索数据在两个日期之间。
使用参数化查询!
As Sinisa Hajnal says, custom format does not affect the actual date, just the way it is displayed and input.
When you set or retrieve the Value property it uses a DateTime property, which you can pass directly to Access (with or without the Date property which just strips time info) by using a parametrized query and passing the DateTime value without conversion. That way there is no confusion about date formats between systems.
BTW: It's considered bad practice to "force" a date format on user: you should normally use the Culture specific date form at for their machine, otherwise they can get confused as well!
"then how can i search data between two dates."
Using a parametrised query!
Using con As New SqlConnection(strConnect)
con.Open()
Using cmd As New SqlCommand("SELECT iD FROM myTable WHERE myDateColumn BETWEEN @STARTDATE AND @ENDDATE", con)
cmd.Parameters.AddWithValue("@STARTDATE", DateTimePicker1.Value)
cmd.Parameters.AddWithValue("@ENDDATE", DateTimePicker2.Value)
Using reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
Dim UserId As Integer = CInt(reader("iD"))
...
End While
End Using
End Using
End Using