开发者

How to add Timestamp value in SQL database by using LINQ

My database table have a Timestamp column and i am using LINQ for insert, update data in SQL database. Now i want to add the timestamp value in my da开发者_如何学Pythontabase but i don't know how?

I seems various examples on net and they didn't write any code for inserting value in database it automatically inserted on insertion data. while i saw in the table there is also no default value assign then how timestamp insert into database by uisng LINQ??


I assume you mean that you have a table with a column that has a Timestamp data type. My answer assumes this is what you mean.

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.


for example you have following model class creted for Table User

public partial class User
{
    public int UserID{get;set;}
    public string UserName{get;set;}
    public DateTiem TimeStamp{get;set;}
}

you can write something like

User user = new User{UserName = "xyz", TimeStamp = DateTime.Now};
MyDataContext context = new MyDataContext();
context.Users.InsertOnSubmit(user);
context.SubmitChanges();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜