[英]Insert DateTime from JSON response in MySQL DB using C#
I've searched this for what I feel to be a lot but all the threads appear to be looking to insert he Date Time now, which I am not.. 我已经搜索了很多东西,但是所有线程似乎都希望现在插入“ Date Time”,但我不是。
I currently receive a Date Time in the following format: 我目前收到以下格式的日期时间:
"Date":"2018-03-03T11:00:00Z"
I'm attempting to insert into a MySQL database where the column data set is DATETIME 我正在尝试插入列数据集为DATETIME的MySQL数据库中
I'm using the following INSERT statement: 我正在使用以下INSERT语句:
"INSERT INTO tblname (col1, col2) VALUES (@Name, @Date) ON DUPLICATE KEY UPDATE col1 = @Name";
Supplying the values via the AddWithValue parameter as follows: 通过AddWithValue参数提供值,如下所示:
Command.Parameters.AddWithValue("@Date", Jo["Results"][x]["Date"]);
This always results in me receiving the following within he database: 这总是导致我在数据库中收到以下信息:
0000-00-00 00:00:00
A full modified version (to keep it short) of the code used is below: 下面是所使用代码的完整修改版本(为简洁起见):
using MySql.Data.MySqlClient;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;
using System.Text;
namespace stackoverflow
{
class forstackoverflow
{
public static int x = 0;
public static JObject Jo { get; set; }
static void Main()
{
string apiUrl = "REMOVED URL";
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader readerStream = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
string json = readerStream.ReadToEnd();
readerStream.Close();
var Jo = JObject.Parse(json);
JArray id = (JArray)Jo["Results"];
for (int x = 0; x < id.Count; x++)
{
string sqlstring = "sql already highlighted.";
MySqlConnection Connection = null;
const string ConnectionString = @"REMOVED CONNECTION DETAILS";
Connection = new MySqlConnection(ConnectionString);
MySqlCommand Command = new MySqlCommand(sqlstring, Connection);
Command.Parameters.AddWithValue("@Name", (string)Jo["Results"][x]["name"]);
Command.Parameters.AddWithValue("@Date", (string)Jo["Results"][x]["date"]);
Connection.Open();
Command.ExecuteNonQuery();
Connection.Close();
Connection.Dispose();
Console.WriteLine((string)Jo["Results"][x]["name"] + " was inserted or updated in the database.");
}
}
}
}
}
When I try to insert the DateTime 2018-03-03T11:00:00Z
into my database (Which is an actual DATETIME
type) I get the following error: 当我尝试将DateTime
2018-03-03T11:00:00Z
插入我的数据库(这是实际的DATETIME
类型)时,出现以下错误:
Warning: #1265 Data truncated for column 'date' at row 1
I assume your database is handling this error and just defaulting to 0000-00-00 00:00:00
. 我认为您的数据库正在处理此错误,并且仅默认为
0000-00-00 00:00:00
。 MySQL DATETIME
needs to be in the format of yyyy-MM-dd HH:mm:ss
. MySQL
DATETIME
必须采用yyyy-MM-dd HH:mm:ss
的格式。 That means that if you modify your example and remove the T
and Z
from it, as well as put a space in between the date and time then you can insert it into your Database. 这意味着,如果您修改示例并从中删除
T
和Z
,并在日期和时间之间放置一个空格,则可以将其插入数据库中。
You can put this in the correct format simply by using: 您只需使用以下命令即可将其设置为正确的格式:
var test = "2018-03-03T11:00:00Z";
test = test.Replace("T", " ").Replace("Z", " ");
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.