在SQL Server中插入ISO日期 [英] Inserting ISO date in sql server

查看:107
本文介绍了在SQL Server中插入ISO日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在c#中运行以下代码时

When I run the below code in c#

string dt = "2017-07-09T17:50:21.000-0500"; 
string date = DateTimeOffset.Parse(dt).DateTime.ToString("yyyy-MM-dd HH:mm");

将其作为输出


2017-07-09 17:50

2017-07-09 17:50

但在将其插入数据库时​​会增加+5小时时间并插入为

but on inserting the same to database it is adding +5 hrs to the time and inserting as


2017-07-09 22:50

2017-07-09 22:50

有人可以帮我在数据库中插入同一日期(2017-07-09 17:50)

could someone please help me to insert the same date (2017-07-09 17:50) to database

更新1

这是我完整的简化代码

DataSet dSet = new DataSet();
DataTable dTable = new DataTable();
dTable.Columns.Add("created_on", typeof(DateTime));

foreach (var item in items)
{
DataRow dRow = dTable.NewRow();
dRow["created_on"] = DateTimeOffset.Parse(item.createdon).DateTime;
//-- in item.createdon i get the date like this "2017-07-09T17:50:21.000-0500";
dSet.Tables[0].Rows.Add(dRow);
}

完成循环后,通过存储过程将数据作为xml发送到数据库

after completing the loop,sending the data to database as xml via stored procedure to insert.

插入数据库后,该行将使用以下日期更新

After inserting in database the row is updated with the below date



2017-07-09 22:50

2017-07-09 22:50



推荐答案

@dlatikay!根据您的第一条评论,我将数据类型从DateTime更改为string并成功了。

@dlatikay! based on your first comment i changed the datatype from DateTime to string and it worked.

dTable.Columns.Add("created_on", typeof(string));

当数据类型为DateTime时,结果如预期般到达,但插入sql时的值为现在,当我将值作为字符串发送到sql时,它将该字符串转换为DateTime并成功了。

When the datatype is DateTime,the result is coming as expected,but on insertion to sql the value is changing.Now, while iam sending the value as string to sql,it is converting that string to DateTime and it worked.

谢谢大家的答复。

这篇关于在SQL Server中插入ISO日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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