Hello I have a String of this type: 20160104 I should convert it into format of SqlServer datetime with c#, how can I do? I have try with this:
DataUltimaVariazione = Convert.ToDateTime("20160104");
but visual studio give me an error
You can use DateTime.ParseExact (https://msdn.microsoft.com/de-de/library/system.datetime.parseexact(v=vs.110).aspx) to convert a string to datetime:
DateTime dateTime = DateTime.ParseExact("20160104", "yyyyMMdd",
CultureInfo.InvariantCulture);
You could also use DateTime.TryParseExact (https://msdn.microsoft.com/de-de/library/h9b85w22(v=vs.110).aspx) which gives you more control on wrong formatted datetime strings:
DateTime dateTime;
if (DateTime.TryParse("20160104", "yyyyMMdd",
CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
{
Console.WriteLine(dateTime);
}
dateordatetimecolumn, retrieve it as DateTime, don't convert it to textDate.ParseExact(dateString, "yyyyddMM", provider)yyyyMMddin SQL Server. Mixing up this order would show an unusual temperament on the part of the file's author. Think Hannibal Lecter. Or that guy that types � in textboxes