简体   繁体   English

存储过程的结果在SQL和C#中不同

[英]Stored Procedure results different in SQL and C#

I am calling a stored procedure in my code and the results are being loaded into a DataTable, here is a column from the Datatable. 我在代码中调用存储过程,结果被加载到DataTable中,这是Datatable中的一列。

代码结果列

However, the time portion of the date is not supposed to be there. 但是,日期的时间部分不应该在那里。 In the stored procedure, I am doing this: "Cast(loan.OpenedDate as date) as OpenedDate". 在存储过程中,我正在这样做:“将Cast(loan.OpenedDate作为日期)作为OpenedDate”。 In SQL if I execute this stored procedure, it displays correctly as shown here. 在SQL中,如果我执行此存储过程,它将正确显示,如下所示。

SQL结果列

Why would the results differ if the procedure is the same? 如果程序相同,为什么结果会不同?

C#中没有日期类型,只有DateTime,因此您的SQL Date结果将转换为DateTime。

That's Because in Code it has DataType of DateTime because C# Doesn't have Date DataType 那是因为在代码中它具有DateTime的数据类型,因为C#没有Date数据类型

So what you do is Use DateTime.ToShortDateString Method 所以您要做的是使用DateTime.ToShortDateString方法

Eg 例如

DateTime dateToDisplay = new DateTime(2009, 6, 1, 8, 42, 50);
dateToDisplay.ToShortDateString()

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

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