-1

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

4

6 回答 6

1

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

如果您使用的是实体框架,您可以查看:如何使用实体框架向数据库服务器询问当前日期时间?

如果您使用的是 Linq to Sql,您可以查看:如何在 Linq to SQL 表达式中使用 SQL 的 GETDATE() 和 DATEADD()?

于 2012-12-06T12:04:26.217 回答
0

在 AttendenceDataContext 中编写此代码片段:

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

接着

 AttenDate = AttendenceDataContext.GetServerDate();
于 2012-12-06T12:03:30.053 回答
0
[Function(Name = "GetDate", IsComposable = true)]
public System.DateTime GetServerDate()
{
    return ((System.DateTime)(this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod()))).ReturnValue));
}
于 2015-06-17T07:57:08.783 回答
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;
}

我正在使用它与实体框架一起使用

于 2018-09-10T11:10:55.310 回答
0

你可以试试这个:

                using (AttendenceDataContext dboContext = new AttendenceDataContext())
                {
                    var dQuery = dboContext.Database.SqlQuery<DateTime>("SELECT getdate()");
                    return dQuery.AsEnumerable().First();
                }
于 2019-10-10T02:35:12.227 回答
-1

你有没有尝试过

System.DateTime.Now

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

请查看以下页面以获取更多信息:

日期时间现在属性

于 2012-12-06T11:54:19.870 回答