1

I am using Entity framework and have 1 field in database AddedDate that is DateTime and not null, so I need to pass DateTime value.

But the problem is I have to pass DB Server datetime. How can I manage in this sceario or how can I get DB Server datatime to pass this.

I need to some unique solution, because I am this on many forms.

Edit: I need DB server Datetime upon insertion/updation in my application so that I can pass to entity framework object.

Thanks

Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • 1
    What is, for you, a `DB Server datetime`? Is that a SQL DateTime? Is that an SQL datetime? Or DB2, Oracle? Is that the internal datetime or something else? Do you have an example of such datetime vs a current date/time? – Abel Jul 14 '10 at 09:58
  • Do you mean the current time on the DB server when your insert/update operation is executed? – jevakallio Jul 14 '10 at 09:58
  • yes, I mean current Datetime of DB Server, when I am insering and updating – Muhammad Akhtar Jul 14 '10 at 10:01
  • try the second answer http://stackoverflow.com/questions/200617/how-do-i-use-sqls-getdate-and-dateadd-in-a-linq-to-sql-expression – MrFox Jul 14 '10 at 10:11

5 Answers5

4

Since you are using entity framework, you can do something like this:

var dateQuery = yourDbContext.CreateQuery<DateTime>("CurrentDateTime() ");
DateTime dateFromSql = dateQuery .AsEnumerable().First();
schellack
  • 10,144
  • 1
  • 29
  • 33
0

In general, if you use the entity framework and you use DateTime in a field, it will automatically do the back/forth conversion for you, just the same way it does so for integers, doubles etc.

Unless you mean something special, i.e., a char[40] field that must be filled with a DateTime value of a particular format.

Abel
  • 56,041
  • 24
  • 146
  • 247
0

You can get database server date and time by running SELECT GETDATE()) script.

Consider you have a table with 4 colums - the first 3 being strings and the last datetime, You can solve your issue by issueing INSERT SQL like this:

INSERT INTO myTable VALUES ('x', 'y', 'z', SELECT GETDATE())

Nagesh
  • 1,288
  • 3
  • 22
  • 46
0

Can't you use a stored procedure so you can get DB server Datetime very easily.

Johnny
  • 1,555
  • 3
  • 14
  • 23
0

Just use getdate() in your query. For example:

INSERT INTO your_table (AddedDate, ...other columns) VALUES (getdate(), ...other values)

This basically asks the server to insert its own current date into the field; there's no need for you to retrieve it locally.

Matt Gibson
  • 37,886
  • 9
  • 99
  • 128