使用linq获取SQL Server日期

问题描述 投票:-1回答:5

在WCF服务中,我使用Linq类。我的代码如下:

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
};

我在wcf服务中编写代码可以在我的操作契约方法中插入getsysdatefrom。我的wcf服务代码如下:

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
            {
            }
        }
    }
}

在AttenDate中我想获得microsoft sql server的当前日期。怎么弄?

c# asp.net wcf linq sql-server-express
5个回答
1
投票

听起来像一个重复的问题。

如果您正在使用Entity Framework,可以查看:How to ask database server for current datetime using entity framework?

如果您正在使用Linq to Sql,您可以查看:How do I use SQL's GETDATE() and DATEADD() in a Linq to SQL expression?


0
投票

在AttendenceDataContext中编写此代码段:

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

然后

 AttenDate = AttendenceDataContext.GetServerDate();

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

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;
}

我正在使用这个与实体框架一起工作


-1
投票

你有没有尝试过

System.DateTime.Now

这将返回当前的日期和时间。

有关更多信息,请查看以下页面:

Date Time Now Property

© www.soinside.com 2019 - 2024. All rights reserved.