-1

In a WCF service I use Linq class. My code is as follows:

AttendenceDataContext projectDataContext=new AttendenceDataContext();
var brAttendence = new BR_Attendance()
{
    SupId =1,
    AttenDate=from w in projectDataContext.gete,
    InTime =,
    OutTime =,
    ImageName =,
    ImageUrl =,
    PresentBR =,
    AbsentBR =,
    Active = true
};

I write the code in a wcf service can i insert getsysdatefrom inside my operation contract method. My wcf service code is as follows:

namespace ServiceHost
{
    [ServiceContract(Namespace = "")]
    [SilverlightFaultBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class UploadService
    {
        [OperationContract]
        public bool Upload(ImageFile image)
        {
            FileStream fileStream = null;
            BinaryWriter writer = null;
            string filePath;

            try
            {
                filePath = HttpContext.Current.Server.MapPath(".") + "\\Picture\\" + image.ImageName;
                if (image.ImageName != string.Empty)
                {
                    fileStream = File.Open(filePath, FileMode.Create);
                    writer = new BinaryWriter(fileStream);
                    writer.Write(image.Imagestream);
                }
                if (fileStream != null)
                    fileStream.Close();
                if (writer != null)
                    writer.Close();

                AttendenceDataContext projectDataContext = new AttendenceDataContext();
                var brAttendence = new BR_Attendance()
                {
                    SupId = 1,
                    AttenDate = from w in projectDataContext.gete,
                    InTime =,
                    OutTime =,
                    ImageName =,
                    ImageUrl =,
                    PresentBR =,
                    AbsentBR =,
                    Active = true
                };

                return true;
            }
            catch (Exception)
            {
                if (fileStream != null)
                    fileStream.Close();
                if (writer != null)
                    writer.Close();
                return false;
            }
            finally
            {
            }
        }
    }
}

In AttenDate I want to get the microsoft sql server current date. How to get it?

Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
decoder
  • 886
  • 22
  • 46
  • If you run sql server in same server machine, no issues with DateTime.Now in c# code. Both are same time. Else call a helper sql method that hits database and gives you the time. But we cant tell the difference between the actual time and the output time returned by function. – Murali Murugesan Dec 06 '12 at 12:00
  • SELECT CURRENT_TIMESTAMP GO SELECT {fn NOW()} GO SELECT GETDATE() GO i want to get datetime directly from linq. – decoder Dec 06 '12 at 12:03

6 Answers6

1

Sounds like a duplicated question.

If you're using Entity Framework, you could take a look at: How to ask database server for current datetime using entity framework?

If you're using Linq to Sql, you could take a look at: How do I use SQL's GETDATE() and DATEADD() in a Linq to SQL expression?

Community
  • 1
  • 1
Glauco Vinicius
  • 2,527
  • 3
  • 24
  • 37
0

Write this Code snippet in AttendenceDataContext :

Function(Name = "GetDate", IsComposable = true)]
 public System.DateTime GetServerDate()
 {
   return ((System.DateTime)(this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod()))).ReturnValue));
 }

And Then

 AttenDate = AttendenceDataContext.GetServerDate();
skjcyber
  • 5,759
  • 12
  • 40
  • 60
0
[Function(Name = "GetDate", IsComposable = true)]
public System.DateTime GetServerDate()
{
    return ((System.DateTime)(this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod()))).ReturnValue));
}
dhh
  • 4,289
  • 8
  • 42
  • 59
Asghar
  • 2,336
  • 8
  • 46
  • 79
  • 1
    Please explain what you did instead of just adding code snippets. That way, the OP (and everybody else) will get a better understanding of your solution. – dhh Jun 17 '15 at 08:43
0
public DateTime getServerTime()
{
    workcalendar2Entities1 db = new workcalendar2Entities1();
    var con = db.Database.Connection;
    var cmd = con.CreateCommand();
    cmd.CommandText = "SELECT SYSDATETIME()";
    con.Open();
    var datetime = (DateTime)cmd.ExecuteScalar();
    con.Close();
    return datetime;
}

i'm using this which is working with entity framework

Mahdi Khalili
  • 1,025
  • 15
  • 32
0

You can try this:

                using (AttendenceDataContext dboContext = new AttendenceDataContext())
                {
                    var dQuery = dboContext.Database.SqlQuery<DateTime>("SELECT getdate()");
                    return dQuery.AsEnumerable().First();
                }
Joe
  • 349
  • 2
  • 3
-1

Have you tried

System.DateTime.Now

That will return the current date and time.

Have a look at the following page for more information:

Date Time Now Property

Gaz Winter
  • 2,924
  • 2
  • 25
  • 47