What does a timestamp in T-Sql mean in C#?

后端 未结 4 1563
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 11:07

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:

相关标签:
4条回答
  • 2020-12-03 11:30

    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".

    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

    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#.

    0 讨论(0)
  • 2020-12-03 11:40

    According to this post

    you'll have to cast it to a byte array.

    byte[] dt = dataReader["dt"] as byte[];
    
    0 讨论(0)
  • 2020-12-03 11:42

    when retrieving from database

    string dt = Convert.ToBase64String(dataReader["dt"] as byte[]);
    

    when passing into database

    new SqlParameter("@dt", Convert.FromBase64String(dt))
    
    0 讨论(0)
  • 2020-12-03 11:44

    It's a byte[], with a Length of 8.

    0 讨论(0)
提交回复
热议问题