There is no such thing as a "DateTime of type yyyyMMdd"; DateTime is just a large integer indicating the amount of time in an era - it has no format. But this is normal, since you should still use parameterized TSQL, so just add a DateTime as the DbParameter value and it will be passed to db in an unambiguous way (do not use string concatenation to create the TSQL command):
DbParameter param = cmd.CreateParameter(); param.ParameterName = "@foo"; param.DbType = DbType.DateTime; param.Value = yourDateTime;
or for SqlCommand :
cmd.Parameters.Add("@foo", SqlDbType.DateTime).Value = yourDateTime;
If you really need a string, just use the string directly as the parameter [n] [var] char.
In addition, in this case, I will use an invariant culture for parsing the date (since the culture is not displayed in the format):
DateTime yourDateTime = DateTime.ParseExact(dateString, "yyyyMMdd", CultureInfo.InvariantCulture);
From the conversation, it seems you might also need to go from DateTime to a string, in which case just cancel it:
string dateString = yourDateTime.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
Marc gravell
source share