How to add Timestamp value in SQL Server 2005 by using LINQ

笑着哭i 提交于 2019-12-12 04:42:15

问题


My database table have a Timestamp column named as inTime and i am using LINQ for insert, update data in SQL server. Now i want to add the timestamp value in my database but i don't know how to insert?

 spaBL.attendance obj = new spaBL.attendance();
            obj.FK_employeeId = 1;
            obj.inTime =[what to write here??]

inTime is of timestamp type.


回答1:


You cannot update a timestamp. See here. And indeed, as TomTom indicated, it doesn't contain actual time information.




回答2:


You do not provide values for columns with a Timestamp (Rowversion) data type. SQL Server will provide a value for you automatically (and it won't be a datetime value). Therefore you do not need to be concerned with having Linq To SQL insert a Timestamp value for you. In fact, you cannot do it. SQL Server will do it for you. You can however, retrieve the value of a Timestamp column. I believe the corresponding C# type will be System.Data.Linq.Binary.

How to add Timestamp value in SQL database by using LINQ

hope it helps




回答3:


Timestamp as in SQL Server Timestamp data type?

;) If I got a cent every time someone did not read the documentation and thought that is a TIME STAMP I would be more rich than bill gates.

Timestamp data time has NO TIME INFORMATION IN IT. It is a running version number, legacy to Sybase SQL Server where SQL Server from Microsoft originated and a totally borked design.

So, no, you CAN NOT SET THAT FIELD, sorry, and it has no usable value except to see whether it changed (then the row was updated).

The documentation is explicitly clear on that, even if some people think reading is maybe a lost art:

http://msdn.microsoft.com/en-us/library/ms182776(v=sql.90).aspx

Is a data type that exposes automatically generated, unique binary numbers within a database. timestamp is generally used as a mechanism for version-stamping table rows.

There is no way for you to set it. CHange your LINQ setup to not update this column.




回答4:


Timestamp values are auto generated by the server on inserts. You should insert rows to the table without supplying a value for the timestamp column. Sql server will then generate the value for the column.

Note the columns of type timestamp does NOT contain values that can be parsed as a DateTime. The idea behind timestamp columns is that they can be used to check if a row has been updated between fetching the row and trying to update the row with new values.



来源:https://stackoverflow.com/questions/11289114/how-to-add-timestamp-value-in-sql-server-2005-by-using-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!