T-Sql 中的时间戳在 C# 中是什么意思? [英] What does a timestamp in T-Sql mean in C#?

查看:35
本文介绍了T-Sql 中的时间戳在 C# 中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个模型对象来保存一个 Sql Server 行,除了 T-Sql/SqlServer 时间戳之外,我完全理解如何做到这一点.该表定义为:

I'm trying to develop a model object to hold a Sql Server row, and I understand perfectly how to do this except for the T-Sql/SqlServer timestamp. The table is defined as:

CREATE TABLE activity (
activity_id int
, ip_address varchar(39)
, user_id varchar(255)
, message_text
, dt timestamp
)

当我将表格行解析为对象时,对于 int 或字符串,我希望执行以下操作:

When I resolve a table row to my object, for an int or a string I would expect to do something like:

ActivityID = (int)dataReader["activity_id"];
IPAddress = (string)dataReader["ip_address"];

但是我该怎么处理时间戳列呢?我在任何地方都找不到时间戳"数据类型.我知道 Sql Server 将时间戳存储为 8 字节二进制文件,但这通常相当于 .NET 中的什么?

But what do I do about the timestamp column? There's no "timestamp" datatype that I can find anywhere. I know that Sql Server stores timestamp as an 8 byte binary, but what is this normally the equivalent of in .NET?

编辑添加: 一点额外信息...这是从我们大型机上的 DB2 表返回的一行,该表通过 Sql Server 视图传来.Rowversion"不是一个选项,DB2 将该列作为时间戳传递.如果时间戳和行版本相同,也许我可以将其视为一个,否则我会被时间戳卡住.

Edited to add: A little extra information... this is a row being returned from a DB2 table on our mainframe coming through a Sql Server view. "Rowversion" isn't an option, and DB2 is handing the column off as a timestamp. If timestamp and rowversion are identical, maybe I can treat it as one, but otherwise I'm stuck with timestamp.

再次编辑添加:这个项目会让我发疯.至少,这将是一次短途旅行.无论如何,是的@JoelC,这是大型机上 DB2 数据库的 Sql Server 视图.我终于找到了我们的一位 DBA,他轻蔑地解释说,当然"DB2 TIMESTAMP 将 Sql Server 视图视为日期时间.从他的语气中我猜只有菜鸟不知道这一点.这就是为什么他在实际视图中将其命名为日期时间",Duh!(我在示例中给了它一个不同的名称,以免触发对命名约定的评论——实际的数据模型图说它是一个 TIMESTAMP 并将其命名为时间戳).因此,在这种情况下,显然必须将其转换为 DateTime.我想我可能会开始考虑成为一名 DBA,这样我也可以让程序员发疯.抱歉,如果我在这个问题上误导了任何回复者——这是无意的,因为我实际上期望时间戳是一个时间戳.傻我.特别感谢微软将字节数组数据类型命名为时间戳",因为它与日期和时间无关.我不知道哪个响应标记为答案.叹息.

Edited again to add: This project is going to drive me nuts. It will, at least, be a short trip. Anyway, yes @JoelC this is a Sql Server view into a DB2 database on the mainframe. I was finally able to track down one of our DBAs, who explained disdainfully that "of course" a DB2 TIMESTAMP comes across as to a Sql Server view as a datetime. From his tone of voice I guess only noobs don't know this. That's why he named it "datetime" in the actual view, Duh! (I gave it a different name in my example so as to not trigger commentary on naming conventions -- the actual data model diagram says it's a TIMESTAMP and names it timestamp). So, in this event, apparently one must cast it to a DateTime. I think I may begin considering becoming a DBA so that I, too, can drive programmers crazy. Sorry if I misled any of the responders to this question -- it was unintentional, as I was actually expecting a timestamp to be, well, a timestamp. Silly me. Thanks are especially due to Microsoft for naming a byte-array datatype a "timestamp" when it has nothing to do with dates and times. I haven't the vaguest idea which response to mark as the Answer. Sigh.

推荐答案

Sql Server 时间戳数据类型名称混淆.把它想象成一个版本号真的更好—任何地方都没有日期/时间信息.事实上,从 Sql Server 2008 开始,该类型已重命名为rowversion".

The name of the Sql Server timestamp data type is confusing. It's really better to think of it more like a version number — there's no date/time information in there anywhere. In fact, as of Sql Server 2008 the type was renamed to "rowversion".

大多数情况下,您希望在 Sql Server 中输入 datetime 类型,它可以轻松映射到 C# 中的 DateTime,或者可能是 datetime2 如果您有支持它的 Sql Server 版本.关于时间戳类型的文档在这里:
http://msdn.microsoft.com/en-us/library/ms182776(v=sql.90).aspx

Most of the time you want the datetime type in Sql Server instead, which maps easily to DateTime in C#, or perhaps datetime2 if you have a version so Sql Server that supports it. Documentation on the timestamp type is here:
http://msdn.microsoft.com/en-us/library/ms182776(v=sql.90).aspx

如果你真的必须按原样映射那个时间戳列,最接近的匹配可能是普通的旧 long(或者可能是 ulong),但我不知道 sql server 如何处理字节排序或 big/little endien-ness vs C#.

If you really must map that timestamp column as-is, the closest match is probably plain old long (or maybe ulong), but I have no idea how sql server handles byte ordering or big/little endien-ness vs C#.

这篇关于T-Sql 中的时间戳在 C# 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆