简体   繁体   English

在SQL Server 2005 Express中查询日期时间

[英]Query a datetime in SQL Server 2005 Express

How to query the rows based on DateTime in SQL Server 2005 Express installed on windows server 2008 r2? 如何在Windows Server 2008 R2上安装的SQL Server 2005 Express中基于DateTime查询行?

I have code that selects rows based on from and to date values. 我有一些代码,用于根据from和date值选择行。 It's working in my Windows 7 system . 它可以在Windows 7系统中运行。 But, It's not working on Windows Server 2008 R2 ... Any help 但是,它在Windows Server 2008 R2上不起作用...任何帮助

this.filterreportTableAdapter.FillBy(this.cRdataset.filterreport_datatable, fromdate, todate);

and my SQL query is 我的SQL查询是

SELECT * 
FROM tablename 
WHERE DATE BETWEEN @fromdate and @todate

It sounds like date/time values are being passed as strings. 听起来日期/时间值作为字符串传递。 This is always a bad idea, and formatting issues abound. 这总是一个坏主意,并且格式问题很多。 The best approach here is simply: use correctly-typed parameters. 最好的方法很简单:使用正确键入的参数。 if both client and server know it is a DateTime, then it is passed as a primitive vaue, not a string - thus no formatting problems. 如果客户端和服务器都知道它是一个DateTime,则将它作为原始值而不是字符串进行传递-因此没有格式问题。

Make sure that fromdate and todate in the c# are DateTime , and that @fromdate , @todate and tablename.DATE in the TSQL are datetime . 确保fromdatetodate在C#中的DateTime ,以及@fromdate@todatetablename.DATE在TSQL的datetime

请设置与Windows 7上相同的日期时间格式,例如:dd / MM / yyyy

it seems that you have a date format mistake when passing date parameters. 似乎在传递日期参数时出现日期格式错误。 there are two solutions: first, you can try to use date format that works everywhere that is: YYYYMMDD ex : 20120722. another solution is to make a DateDiff in sql query Best regard 有两种解决方案:首先,您可以尝试使用在任何地方都可以使用的日期格式,即:YYYYMMDD ex:20120722。另一种解决方案是在sql查询中创建DateDiff。

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

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