使用 C# 时丢失 DateTimeOffset 精度

发布于 2024-08-25 19:36:40 字数 581 浏览 12 评论 0原文

我有一个 SQL Server 表,其中包含 DateTimeOffset(2) 类型的 CreatedDate 字段。
表中的示例值是 2010-03-01 15:18:58.57 -05:00

作为示例,我从 C# 中检索该值,如下所示:

var cmd = new SqlCommand("SELECT CreatedDate FROM Entities WHERE EntityID = 2", cn);  
var da = new SqlDataAdapter(cmd);  
DataTable dt =new DataTable();  
da.Fill(dt);

我查看该值:
MessageBox.Show(dt.Rows[0][0].ToString());

结果是 2010-03-01 15:18:58 -05:00,缺少数据库中存储的 .57。

如果我在“监视”窗口中查看 dt.Rows[0][0],我也看不到 .57,因此看起来它已被截断。

有人可以解释一下吗?我需要使用日期与数据库中的其他记录进行匹配,并且需要 0.57。

谢谢!
达尔维斯

I have a SQL Server table with a CreatedDate field of type DateTimeOffset(2).
A sample value which is in the table is 2010-03-01 15:18:58.57 -05:00

As an example, from within C# I retrieve this value like so:

var cmd = new SqlCommand("SELECT CreatedDate FROM Entities WHERE EntityID = 2", cn);  
var da = new SqlDataAdapter(cmd);  
DataTable dt =new DataTable();  
da.Fill(dt);

And I look at the value:
MessageBox.Show(dt.Rows[0][0].ToString());

The result is 2010-03-01 15:18:58 -05:00, which is missing the .57 that is stored in the database.

If I look at dt.Rows[0][0] in the Watch window, I also do not see the .57, so it appears it has been truncated.

Can someone shed some light on this? I need to use the date to match up with other records in the database and the .57 is needed.

Thanks!
Darvis

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

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

发布评论

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

评论(2

无法回应 2024-09-01 19:36:40

SQL Server 没有毫秒精度。

这是一篇详细介绍的文章:

高级 SQL Server DATE 和 DATETIME 处理

编辑

我猜 C# 不应该表现出相同的行为。请记住,除非您指定格式提供程序,否则 DateTime.ToString() 方法不包含毫秒。

Watch 窗口还会向您显示 ToString()。尝试设置断点并使用检查器获取正在存储的更详细信息,并查看是否存在毫秒。

SQL Server doesn't have millisecond precision.

Here's an article that goes into the details:

Advanced SQL Server DATE and DATETIME Handling

EDIT

I'm guessing C# shouldn't show the same behavior. Keep in mind that the DateTime.ToString() method doesn't include milliseconds unless you specify a Format Provider.

The Watch window would also be showing you ToString(). Try setting a Breakpoint and use the Inspector to get the more detailed information being stored and see if you mililseconds are there.

一身仙ぐ女味 2024-09-01 19:36:40

Fill 应该更改数据集中的数据以匹配数据源中的数据 - 您是否尝试过任何其他方法来检索数据以查看其他类是否以您希望的格式检索数据?简单的测试,如sqldatasource/gridview等

Fill should change the data in the dataset to match the data in the datasource -- have you tried any other methods to retrieve the data to see if other classes retrieve the data in the format you are hoping for? Simple tests like sqldatasource/gridview, etc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文