1

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 database 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??

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Gaurav - I don't know, but why would you want to do this? This column simply contains an internal (to SQL Server) row version number for concurrency purposes. You shouldn't need or care to know what this value is. L2S can use it for concurrency checking however.
0

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();

6 Comments

but in many examples timestamp value inserted automatically. How?
If you wanted to be clever, you could implement INotifyPropertyChanged and have the properties update the TimeStamp when they change rather doing it by hand
Could be a trigger in the SQL Server?
@Guarav i normally set default value of TimeStamp to getdate() or similar function in sql server and then if you do not provide an explicit value, default value is used instead
I checked this but in LINQ Timestamp SQL datatyper converts into System.Data.Linq.Binary in c# LINQ so how would be we can use Datetime here???
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.