【问题标题】:C# Select in SQL Server just using the Date with sql parametersC# 在 SQL Server 中选择仅使用带有 sql 参数的日期
【发布时间】:2016-08-22 14:23:58
【问题描述】:

我目前正在尝试使用Datetime 类型的参数在我的SQL Server 数据库中执行Select。但是我需要这个参数只有YYYY-MM-DD date 格式,因为我正在使用 SQL 中的以下查询并且它正在工作:

select 
    idCliente, DescCliente, DescAnalista , StatusChamado , DataAtendimento 
from 
    Atendimento 
where 
    cast ([DataAtendimento] as date) = '2016-04-27';

我阅读了几篇帖子,表明我使用了DbType.ToString,但是在运行它时会产生错误,声称无法将字符串转换为日期/时间格式。

这就是我使用SqlParameter的方式:

DateTime date;
date = Data;

try
{
    sqlClient.Command.Parameters.AddWithValue("@adate", date);
    sqlClient.Command.CommandText = @" select idCliente, DescCliente, DescAnalista , StatusChamado , DataAtendimento from Atendimento where cast ([DataAtendimento] as date) = '@adate';";

我需要你们的帮助,我找不到任何可以执行此选择的方法

【问题讨论】:

  • 删除= '@adate';中的''
  • 我不敢相信我因为这些引言而被逮捕了几个小时,效果很好,谢谢
  • 我们都去过那里。 =)

标签: c# sql-server date datetime sqlparameter


【解决方案1】:

您必须删除= '@adate' 中的''。使用'',它可以有效地将您的查询变成:

select 
    idCliente, DescCliente, DescAnalista , StatusChamado , DataAtendimento 
from Atendimento 
where cast ([DataAtendimento] as date) = '@date';

这意味着cast([DateAtendimento] as date) 与字符串'@date' 进行比较,因此转换为日期错误。

【讨论】:

  • 这正是发生的事情,我认为我没有几个小时研究并试图转换日期,认为问题出在了,谢谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
  • 2017-12-20
  • 1970-01-01
相关资源
最近更新 更多