【发布时间】:2010-11-05 04:08:52
【问题描述】:
如何将日期时间值插入到列类型为日期时间的 SQL 数据库表中?
【问题讨论】:
如何将日期时间值插入到列类型为日期时间的 SQL 数据库表中?
【问题讨论】:
以下应该可行,并且是我的建议(参数化查询):
DateTime dateTimeVariable = //some DateTime value, e.g. DateTime.Now;
SqlCommand cmd = new SqlCommand("INSERT INTO <table> (<column>) VALUES (@value)", connection);
cmd.Parameters.AddWithValue("@value", dateTimeVariable);
cmd.ExecuteNonQuery();
【讨论】:
DateTime time = DateTime.Now; // Use current time
string format = "yyyy-MM-dd HH:mm:ss"; // modify the format depending upon input required in the column in database
string insert = @" insert into Table(DateTime Column) values ('" + time.ToString(format) + "')";
并执行查询。
DateTime.Now 是插入当前日期时间..
【讨论】:
datetime2 在 SQL Server 中默认为 7 小数秒的精度。更好的格式是yyyy-MM-dd HH:mm:ss.FFFFFFF。
使用 yyyy-mm-dd hh:mm:ss 格式更标准(IE:2009-06-23 19:30:20)
使用它您不必担心日期的格式(MM/DD/YYYY 或 DD/MM/YYYY)。它适用于所有人。
【讨论】:
using (SqlConnection conn = new SqlConnection())
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "INSERT INTO <table> (<date_column>) VALUES ('2010-01-01 12:00')";
cmd.ExecuteNonQuery();
}
我写这些东西已经有一段时间了,所以这可能并不完美。但总体思路就在那里。
警告:这是未经消毒的。您应该使用参数来避免注入攻击。
编辑: 因为乔恩坚持。
【讨论】:
这是一个较老的问题,有一个正确的答案 (please use parameterized queries),我想通过一些时区讨论来扩展它。对于我当前的项目,我对datetime 列如何处理时区很感兴趣,这个问题就是我找到的。
事实证明,他们根本没有。
datetime 列按原样存储给定的DateTime,不进行任何转换。给定的日期时间是 UTC 还是本地都没有关系。
你可以自己看看:
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT * FROM (VALUES (@a, @b, @c)) example(a, b, c);";
var local = DateTime.Now;
var utc = local.ToUniversalTime();
command.Parameters.AddWithValue("@a", utc);
command.Parameters.AddWithValue("@b", local);
command.Parameters.AddWithValue("@c", utc.ToLocalTime());
using (var reader = command.ExecuteReader())
{
reader.Read();
var localRendered = local.ToString("o");
Console.WriteLine($"a = {utc.ToString("o").PadRight(localRendered.Length, ' ')} read = {reader.GetDateTime(0):o}, {reader.GetDateTime(0).Kind}");
Console.WriteLine($"b = {local:o} read = {reader.GetDateTime(1):o}, {reader.GetDateTime(1).Kind}");
Console.WriteLine($"{"".PadRight(localRendered.Length + 4, ' ')} read = {reader.GetDateTime(2):o}, {reader.GetDateTime(2).Kind}");
}
}
}
这将打印什么当然取决于您的时区,但最重要的是,读取的值都将具有Kind = Unspecified。第一个和第二个输出行将因您的时区偏移而不同。第二个和第三个将是相同的。使用"o" format string (roundtrip) 不会为读取的值显示任何时区说明符。
GMT+02:00 的示例输出:
a = 2018-11-20T10:17:56.8710881Z read = 2018-11-20T10:17:56.8700000, Unspecified
b = 2018-11-20T12:17:56.8710881+02:00 read = 2018-11-20T12:17:56.8700000, Unspecified
read = 2018-11-20T12:17:56.8700000, Unspecified
还要注意数据是如何被截断(或四舍五入)到 10 毫秒的。
【讨论】:
您可以将 DateTime 值作为具有特殊格式的字符串发送到 SQL。这个格式是“yyyy-MM-dd HH:mm:ss”
示例:CurrentTime是一个变量,如datetime在SQL中键入。而 dt 是 .Net 中的 DateTime 变量。
DateTime dt=DateTime.Now;
string sql = "insert into Users (CurrentTime) values (‘{0}’)";
sql = string.Format(sql, dt.ToString("yyyy-MM-dd HH:mm:ss") );
【讨论】:
INSERT INTO <table> (<date_column>) VALUES ('1/1/2010 12:00')
【讨论】: