[英]How to create a datetime variable with millisecond in c#? (blazor)
private Task OnProjectCreated()
{
DateTime now = DateTime.Now.AddMilliseconds(1000);
ProjectModel newProject = new ProjectModel
{
ProjectName = "ProjectA",
ProjectUpdated = now
}
_db.InsertProject(newProject);
var currentProject = _db.GetProjectByTime<ProjectModel>(now);
//here, the currentProject object is passing to the parent component(balzor)
return ProjectCreated.InvokeAsync(currentProject);
}
I want to fetch a project from database by DateTime but the DateTime variable 'now' has no milliseconds therefore I get an error like this 'Sequence contains no elements'.
我想通过 DateTime 从数据库中获取一个项目,但是 DateTime 变量 'now' 没有毫秒,因此我收到一个类似'Sequence contains no elements'的错误。 So I have added AddMilliseconds(1000) but it is still not working.
所以我添加了 AddMilliseconds(1000) 但它仍然无法正常工作。 I think that 'now' variable does not match with Project.ProjectUpdated which is saved in the database.
我认为“现在”变量与保存在数据库中的 Project.ProjectUpdated 不匹配。
I have controlled the Project table in sql server management studio and there all ProjectUpdated are saved with milliseconds but when I use 'now' as an arg in _db.GetProjectByTime(now) has no milliseconds.
我已经控制了 sql 服务器管理工作室中的项目表,并且所有 ProjectUpdated 都以毫秒为单位保存,但是当我在 _db.GetProjectByTime(now) 中使用“现在”作为参数时,没有毫秒。 How can I solve it?
我该如何解决? Thank you in advance!
先感谢您!
public ProjectModel GetProjectByTime<ProjectModel>(DateTime dateTime)
{
string sql = $"select * from Projects where ProjectUpdated =
'{dateTime}'";
var data = _accessDb.GetData<ProjectModel>(sql);
return data;
}
the problem is that, ProjectUpdated in db is saved like this:2021-06-07 17:46:28.747 but when I use 'now' as arg in _db.GetProjectByTime(now) it looks like this: 2021-06-07 17:46:28 I mean without milliseconds (as I have controlled by break points at running)
问题是,数据库中的 ProjectUpdated 保存如下:2021-06-07 17:46:28.747 但是当我在 _db.GetProjectByTime(now) 中使用 'now' 作为 arg 时,它看起来像这样:2021-06-07 17 :46:28 我的意思是没有毫秒(因为我在运行时由断点控制)
I changed the string sql to
我将字符串 sql 更改为
string sql = $"select * from Projects where ProjectUpdated = CONVERT(datetime, '{dateTime}')";
and this is working now without problem.
这现在可以正常工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.