[英]DateTime filtering doesn't work
I have an local database in Visual Studio (SQL Server Compact Edition), with a table with a few columns. 我在Visual Studio(SQL Server Compact Edition)中有一个本地数据库,其中包含一些包含几列的表。 One of those columns has the datatype
DateTime
. 其中一列的数据类型为
DateTime
。 When I to insert a row I add the date/time value by using DateTime.Now
in C#. 当我插入一行时,我在C#中使用
DateTime.Now
添加日期/时间值。
When I do a 当我做的时候
SELECT * FROM ...
I see that the DateTime format is 7-5-2012 11:41
. 我看到DateTime格式是
7-5-2012 11:41
。
But now I need to do a select query with a where clause, so that the result is between certain days. 但现在我需要使用where子句进行选择查询,以便结果在某些天之间。 I can't find a way to filter between dates/times, when I use the
DateTime.Now
function to set the between I get 0 rows back. 当我使用
DateTime.Now
函数设置我得到的0行之间时,我找不到在日期/时间之间进行过滤的方法。 But also when I set the between manually by setting 但是当我通过设置手动设置
WHERE date BETWEEN '7-5-2012 00:00' AND '8-5-2012 00:00'
and even then I get 0 values back. 即便如此,我得到0值。
I also tried to do it with SqlCeCommands
but gives is the same problem, 0 rows back. 我也尝试用
SqlCeCommands
做,但是给出了同样的问题,0行回来了。
Does anyone what I'm doing wrong or how I get this to work? 有人做错了什么或者我怎么做这个?
Thank you in advance! 先感谢您!
May be you have wrong date format, you should use query command parameters: 可能是你的日期格式错误,你应该使用查询命令参数:
command.CommandText = "SELECT * FROM ... WHERE date BETWEEN @lowb AND @highb";
command.Parameters.Add( "@lowb", SqlDbType.DateTime, new DateTime(...));
command.Parameters.Add( "@highb", SqlDbType.DateTime, new DateTime(...));
尝试这个:
"..spRis_GetListaDocs @DateFilter = '" + Convert.ToDateTime(txtDate.Text).ToString("yyyy-MM-dd") + "'"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.