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..
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
I'm using the following INSERT statement:
"INSERT INTO tblname (col1, col2) VALUES (@Name, @Date) ON DUPLICATE KEY UPDATE col1 = @Name";
Supplying the values via the AddWithValue parameter as follows:
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.");
}
}
}
}
}
MySqlDbTypeparameter.