如何插入日期时间到SQL数据库中的表? [英] how to insert datetime into the SQL Database table?

查看:1159
本文介绍了如何插入日期时间到SQL数据库中的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能插入日期时间到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 )

这篇关于如何插入日期时间到SQL数据库中的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆