【问题标题】:Get SQL Server date using linq使用 linq 获取 SQL Server 日期
【发布时间】:2012-12-06 11:52:54
【问题描述】:

在 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 当前日期。如何获得?

【问题讨论】:

  • 如果您在同一台服务器机器上运行 sql server,C# 代码中的 DateTime.Now 没有问题。两者是同一时间。否则调用一个帮助 sql 方法来访问数据库并给你时间。但是我们无法分辨实际时间和函数返回的输出时间之间的差异。
  • SELECT CURRENT_TIMESTAMP GO SELECT {fn NOW()} GO SELECT GETDATE() GO 我想直接从 linq 获取日期时间。

标签: c# asp.net wcf linq sql-server-express


【解决方案1】:

听起来像是重复的问题。

如果您使用的是实体框架,您可以查看: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?

【讨论】:

    【解决方案2】:

    在 AttendenceDataContext 中编写此代码 sn-p :

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

    然后

     AttenDate = AttendenceDataContext.GetServerDate();
    

    【讨论】:

    • 我在 wcf 的操作合同中写了 br 出席,我可以在该函数中使用该函数吗?
    • 你可以试试!!在 AttendenceDataContext 类中编写代码 sn-p。
    • 我的出席数据上下文课程在哪里。我是 linq 的新手
    • 那么你是如何创建对象的呢?它是一个用户创建的类。我不认为这是框架的一部分。
    【解决方案3】:
    [Function(Name = "GetDate", IsComposable = true)]
    public System.DateTime GetServerDate()
    {
        return ((System.DateTime)(this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod()))).ReturnValue));
    }
    

    【讨论】:

    • 请解释你做了什么,而不是仅仅添加代码 sn-ps。这样,OP(以及其他所有人)将更好地了解您的解决方案。
    【解决方案4】:
    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;
    }
    

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

    【讨论】:

      【解决方案5】:

      你可以试试这个:

                      using (AttendenceDataContext dboContext = new AttendenceDataContext())
                      {
                          var dQuery = dboContext.Database.SqlQuery<DateTime>("SELECT getdate()");
                          return dQuery.AsEnumerable().First();
                      }
      

      【讨论】:

        【解决方案6】:

        你试过了吗

        System.DateTime.Now
        

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

        查看以下页面了解更多信息:

        Date Time Now Property

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-24
        • 1970-01-01
        • 2021-09-13
        • 1970-01-01
        • 2013-09-27
        • 2016-06-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多