在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的当前日期。怎么弄?
听起来像一个重复的问题。
如果您正在使用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?
在AttendenceDataContext中编写此代码段:
Function(Name = "GetDate", IsComposable = true)]
public System.DateTime GetServerDate()
{
return ((System.DateTime)(this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod()))).ReturnValue));
}
然后
AttenDate = AttendenceDataContext.GetServerDate();
[Function(Name = "GetDate", IsComposable = true)]
public System.DateTime GetServerDate()
{
return ((System.DateTime)(this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod()))).ReturnValue));
}
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;
}
我正在使用这个与实体框架一起工作