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

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

问题描述

我正在尝试开发一个模型对象来保存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?

已编辑添加:一些额外的信息...这是从我们的大型机上通过Sql Server视图的DB2表返回的一行。 Rowversion不是一个选项,DB2正在将列关闭作为时间戳。如果时间戳和rowversion是相同的,也许我可以将它当作一个,否则我被卡住了时间戳。

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这是一个Sql Server视图到大型机上的一个DB2数据库。我终于能够跟踪我们的一个DBA,他们鄙视地解释说,TIMESTAMP当然是一个SQL Server视图作为一个datetime。从他的声音,我猜只有noobs不知道这一点。这就是为什么他在实际视图中命名为datetime,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如何处理字节排序或大/ 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#.

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

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