简体   繁体   English

SQL Server Compact 4.0 DateTimePicker选择日期之间的查询

[英]SQL Server Compact 4.0 DateTimePicker select query between dates

Using Winforms, .Net 4.5. 使用Winforms,.Net 4.5。

I have a form with a DataGridView and in the dataGridView I insert a few values from another form like Client name and Date, I struggled for a few hours to figure it out, I'm using SQL Server Compact 4.0 so it only supports DateTime datatype unlike SQL Server 2008 or so where you can store date without time, so what I need to do is some select between dates query. 我有一个带有DataGridView的表单,并且在dataGridView中,我从另一个表单(如客户端名称和日期)插入了一些值,我花了几个小时才能弄清楚,我使用的是SQL Server Compact 4.0,因此它仅支持DateTime数据类型不像SQL Server 2008那样,您可以在没有时间的情况下存储日期,所以我需要做的是在日期查询之间进行一些选择。

But in SQL Server Compact you can store only the whole value of date like 24/02/2014 12:00 I only need the date and SQL Server Compact won't let me do that. 但是在SQL Server Compact中,您只能存储date的整个值,例如24/02/2014 12:00我只需要日期,而SQL Server Compact不允许我这样做。 I tried using DateTimePicker.Text to send data to SQL Server Compact but with strings you cannot do a select between dates because they are not dates they are strings. 我尝试使用DateTimePicker.Text将数据发送到SQL Server Compact,但是使用字符串您无法在日期之间进行选择,因为它们不是日期,而是字符串。 What can I do from this situation, remember I need only date format I don't need the time, remember also SQL Server Compact cannot store only dates. 在这种情况下我该怎么办,请记住我只需要日期格式,不需要时间,请记住SQL Server Compact也不能只存储日期。 So is there any possible ways to do this select between strings ( dateTimePicker.Text ) ? 那么,是否有任何可能的方法可以在字符串之间进行选择( dateTimePicker.Text )?

Store DateTime in your database - you really have no other choice. DateTime存储在数据库中-您实际上别无选择。

If you don't need time - use only Date portion of DateTime when performing selects. 如果不需要时间,请在执行选择时仅使用DateTime的Date部分。

Edited: 编辑:

I'm not sure how you do your selects, but if it is just text query then 我不确定您如何选择,但是如果只是文本查询,那么

WHERE clause would look like this: WHERE子句如下所示:

"DateColumn >= '" + sd.ToShortDateString() + "' AND DateColumn <= '" + ed.ToShortDateString() + "'";

where sd is a start date and ed is the end date, both of type DateTime in C#, and DateColumn is the column where you keep your dates. 其中sd是开始日期, ed是结束日期,在C#中均为DateTime类型,而DateColumn是保留日期的列。 Same idea applies for inserts. 同样的想法适用于插入。

SELECT would look like this (returns 12/12/2013): SELECT看起来像这样(返回1​​2/12/2013):

select ltrim(str(DATEPART(month, DateColumn))) + ':' + ltrim(str(DATEPART(day, DateColumn))) + ':' + ltrim(str(DATEPART(year, DateColumn))) AS MyDate FROM TableName

Generic use of DATEPART: DATEPART的一般用法:

SELECT DATEPART(month, [DateColumn]) AS Month FROM TableName

More on DATEPART is here 有关DATEPART的更多信息,请点击这里

Also, see this post and this post 另外,请参阅这篇文章这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM