database 使数据库中的日期时间字段自动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2455974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 07:38:34  来源:igfitidea点击:

Making a DateTime field in a database automatic?

databasedatabase-designdatetimesql-server-expressfield

提问by RocketGoal

I'm putting together a simple test database to learn MVC with. I want to add a DateTime field to show when the record was CREATED.

我正在整理一个简单的测试数据库来学习 MVC。我想添加一个 DateTime 字段来显示记录的创建时间。

ID = int
Name = Char
DateCreated = (dateTime, DateTime2..?)

I have a feeling that this type of DateTime capture can be done automatically - but that's all I have, a feeling. Can it be done? And if so how?

我有一种感觉,这种类型的 DateTime 捕获可以自动完成 - 但这就是我的全部,一种感觉。可以做到吗?如果是这样怎么办?

While we're on the subject: if I wanted to include another field that captured the DateTime of when the record was LAST UPDATED how would I do that.

当我们讨论这个主题时:如果我想包含另一个字段来捕获记录上次更新的日期时间,我将如何做。

I'm hoping to not do this manually.

我希望不要手动执行此操作。

Many thanks

非常感谢

Mike

麦克风

回答by Ant

You need to set the "default value" for the date field to getdate(). Any records inserted into the table will automatically have the insertion date as their value for this field.

您需要将日期字段的“默认值”设置为getdate()。插入表中的任何记录都将自动将插入日期作为该字段的值。

The location of the "default value" property is dependent on the version of SQL Server Express you are running, but it should be visible if you select the date field of your table when editing the table.

“默认值”属性的位置取决于您运行的 SQL Server Express 版本,但如果您在编辑表时选择表的日期字段,它应该是可见的。

回答by Kevin

Yes, here's an example:

是的,这是一个例子:

CREATE TABLE myTable ( col1 int, createdDate datetime DEFAULT(getdate()), updatedDate datetime DEFAULT(getdate()) )

You can INSERT into the table without indicating the createdDate and updatedDate columns:

您可以插入表而不指示 createdDate 和 updatedDate 列:

INSERT INTO myTable (col1) VALUES (1)

Or use the keyword DEFAULT:

或者使用关键字 DEFAULT:

INSERT INTO myTable (col1, createdDate, updatedDate) VALUES (1, DEFAULT, DEFAULT)

Then create a trigger for updating the updatedDate column:

然后创建一个触发器来更新 updatedDate 列:

CREATE TRIGGER dbo.updateMyTable 
ON dbo.myTable
FOR UPDATE 
AS 
BEGIN 
    IF NOT UPDATE(updatedDate) 
        UPDATE dbo.myTable SET updatedDate=GETDATE() 
        WHERE col1 IN (SELECT col1 FROM inserted) 
END 
GO

回答by akbar

Just right click on that column and select properties and write getdate()in Default value or binding.like image:

只需右键单击该列并选择属性和编写getdate()Default value or binding。像图片:

enter image description here

在此处输入图片说明

If you want do it in CodeFirstin EFyou should add this attributes befor of your column definition:

如果你想这样做CodeFirstEF你应该在你的列定义之前添加这个属性:

[Databasegenerated(Databaseoption.computed)]

[数据库生成(Databaseoption.computed)]

this attributes can found in System.ComponentModel.Dataannotion.Schema.

这个属性可以在System.ComponentModel.Dataannotion.Schema.

In my opinion first one is better:))

在我看来,第一个更好:))