DateTime运算符数据库 [英] DateTime operators Database
问题描述
大家好
如何找到两个日期值之间的记录?
我的意思是:
我有2个文本框,并且像textbox1 = 1988/5/4
和textbox2 = 2010/5/5
那样填充它们.
当我单击按钮SEARCH
时,我想显示这些日期之间的所有记录.
运算符>" <"不适用于日期.
请帮助
Hi everybody
How can I find records between 2 date values?
My meaninng:
I have 2 text boxes and I fill them like textbox1 = 1988/5/4
and textbox2 = 2010/5/5
.
When I click on button SEARCH
I would like to show all records between these date.
Operator ">" "<" is not work for date.
Please help
推荐答案
为什么要使用TextBoxes而不是DateTimePickers?
在许多情况下,TextBox将失败.有些人使用不同的日期格式(我来自dd-MM-yyyy).有些人为日期设置了不同的数据库,但这只是行不通.
更好的方法如下:
Why are you using TextBoxes and not DateTimePickers?
A TextBox will fail in many cases. Some people use different date formats (dd-MM-yyyy where I come from). Some people have different database settings for dates, it''s just not going to work.
A better approach is the following:
Dim cmd As New OleDbCommand("SELECT WhatEverYouNeed FROM MyTable Where MyDateColumn BETWEEN @Date1 AND @Date2", connection)
cmd.Parameters.Add("@Date1", DateTimePicker1.Value)
cmd.Parameters.Add("@Date2", DateTimePicker2.Value)
' Open connection, run the cmd query and read results.
此处 [ ^ ].
或者,您可以使用ORM,例如实体框架 [LINQ [ ^ ]来查询您的数据库.
How to use Commands, Connections, Adapters, Readers and DataSets etc. can be found here[^].
Alternatively you can use an ORM such as Entity Framework[^] and use LINQ[^] to query against your database.
这篇关于DateTime运算符数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!