Linq-to-SQL 日期格式:查询不起作用

发布于 2024-12-18 00:19:24 字数 852 浏览 1 评论 0原文

我有一个存储过程,当我在 SQL Server 中执行此查询时,

exec SelectOfficeNameGroup  '2011-10-05', '2011-11-09', ''

它返回 110 行。

现在我的 c# 3.5 表单上有两个日期选择器和一个执行查询的按钮,如下所示:

var result = context.SelectOfficeNameGroup(dateTimePickerFrom.Value, dateTimePickerTo.Value, "");

但它只返回 2 行。

我的日期选择器的格式是 MM/DD/YYYY

这是我的程序

ALTER PROC [dbo].[SelectOfficeNameGroup]
  @From datetime,
  @To datetime,
  @OfficeName nvarchar(50)
AS
BEGIN
   SELECT
      ID AS ProductID, OfficeName,  
      SUM(Quantity) AS Quantity, 
      SUM(TotalPrice) AS TotalPrice, 
      Category
   FROM
      ProductLogWithCategory 
   WHERE 
      DateTime BETWEEN @From AND @To AND OfficeName = @OfficeName
   GROUP BY 
      OfficeName, ID, Category

  return
END

有什么建议吗?

I have a stored procedure when I am executing this query in SQL Server

exec SelectOfficeNameGroup  '2011-10-05', '2011-11-09', ''

it returns 110 rows.

Now I have two datepicker on my c# 3.5 form and one button which executes the query like this:

var result = context.SelectOfficeNameGroup(dateTimePickerFrom.Value, dateTimePickerTo.Value, "");

but its only returns 2 rows.

My date picker's format is MM/DD/YYYY

This is my procedure

ALTER PROC [dbo].[SelectOfficeNameGroup]
  @From datetime,
  @To datetime,
  @OfficeName nvarchar(50)
AS
BEGIN
   SELECT
      ID AS ProductID, OfficeName,  
      SUM(Quantity) AS Quantity, 
      SUM(TotalPrice) AS TotalPrice, 
      Category
   FROM
      ProductLogWithCategory 
   WHERE 
      DateTime BETWEEN @From AND @To AND OfficeName = @OfficeName
   GROUP BY 
      OfficeName, ID, Category

  return
END

Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原来是傀儡 2024-12-25 00:19:24

尝试

var result = context.SelectOfficeNameGroup(dateTimePickerFrom.Value.ToString("yyyy-MM-dd"), dateTimePickerTo.Value.ToString("yyyy-MM-dd"), "");

查看DateTime.ToString 文档< /a>


更新

为了安全起见,尝试

ALTER PROC [dbo].[SelectOfficeNameGroup]
  @From varchar(8),
  @To varchar(8),
  @OfficeName nvarchar(50)
AS
BEGIN
   SELECT
      ID AS ProductID, OfficeName,  
      SUM(Quantity) AS Quantity, 
      SUM(TotalPrice) AS TotalPrice, 
      Category
   FROM
      ProductLogWithCategory 
   WHERE 
      DateTime BETWEEN @From AND @To AND OfficeName = @OfficeName
   GROUP BY 
      OfficeName, ID, Category

  return
END

像这样使用它

来自 sql

exec SelectOfficeNameGroup  '20111005', '20111109', ''

来自 linq

var result = context.SelectOfficeNameGroup(dateTimePickerFrom.Value.ToString("yyyyMMdd"), dateTimePickerTo.Value.ToString("yyyyMMdd"), "");

Try with

var result = context.SelectOfficeNameGroup(dateTimePickerFrom.Value.ToString("yyyy-MM-dd"), dateTimePickerTo.Value.ToString("yyyy-MM-dd"), "");

Have a look at the DateTime.ToString documentation


Update

Just to be on the safe side try

ALTER PROC [dbo].[SelectOfficeNameGroup]
  @From varchar(8),
  @To varchar(8),
  @OfficeName nvarchar(50)
AS
BEGIN
   SELECT
      ID AS ProductID, OfficeName,  
      SUM(Quantity) AS Quantity, 
      SUM(TotalPrice) AS TotalPrice, 
      Category
   FROM
      ProductLogWithCategory 
   WHERE 
      DateTime BETWEEN @From AND @To AND OfficeName = @OfficeName
   GROUP BY 
      OfficeName, ID, Category

  return
END

and use it like this

from sql

exec SelectOfficeNameGroup  '20111005', '20111109', ''

from linq

var result = context.SelectOfficeNameGroup(dateTimePickerFrom.Value.ToString("yyyyMMdd"), dateTimePickerTo.Value.ToString("yyyyMMdd"), "");
錯遇了你 2024-12-25 00:19:24
DateTime.Parse('your date in MM/DD/YYYY', new CultureInfo("en-US")).ToString("yyyy-MM-dd")
DateTime.Parse('your date in MM/DD/YYYY', new CultureInfo("en-US")).ToString("yyyy-MM-dd")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文