如何插入日期时间到SQL数据库中的表?数据库中、日期、时间、SQL

2023-09-02 10:37:40 作者:别逼我耍流氓

我怎样才能插入日期时间到SQL数据库中的表? 有没有一种方法来插入此查询通过插入命令在C#/。NET?

How can I insert datetime into the SQL Database table ? Is there a way to insert this query through the insert command in C# / .NET?

推荐答案

日期时间值应该被插入,如果他们是用单引号括起来的字符串:

DateTime values should be inserted as if they are strings surrounded by single quotes:

'20100301'

SQL Server允许对许多接受日期格式,它应该是大多数开发库提供了一系列的类或函数来正确插入日期时间值的情况。但是,如果你正在做手工,用它来区分的日期格式是很重要的日期格式,并使用通用格式为:

SQL Server allows for many accepted date formats and it should be the case that most development libraries provide a series of classes or functions to insert datetime values properly. However, if you are doing it manually, it is important to distinguish the date format using DateFormat and to use generalized format:

Set DateFormat MDY --indicates the general format is Month Day Year

Insert Table( DateTImeCol )
Values( '2011-03-12' )

通过设置日期格式,SQL Server现在假设我的格式为: YYYY-MM-DD 而不是 YYYY-MM-DD

SET DATEFORMAT

SQL Server还可以识别的通用格式,始终是跨preTED以同样的方式:年月日如: 20110312

SQL Server also recognizes a generic format that is always interpreted the same way: YYYYMMDD e.g. 20110312.

如果你是问如何将使用T-SQL的当前日期和时间,然后我会建议使用关键字 CURRENT_TIMESTAMP 。例如:

If you are asking how to insert the current date and time using T-SQL, then I would recommend using the keyword CURRENT_TIMESTAMP. For example:

Insert Table( DateTimeCol )
Values( CURRENT_TIMESTAMP )