There is no such thing as "a DateTime of type yyyyMMdd"; a DateTime is just a large integer, indicating the amount of time in an epoch - it doesn't have a format. But that is fine, since you should be using parametrized TSQL anyway - so just add the DateTime as the value of a DbParameter, and it will be handed to the db in an unambiguous way (don't use string concatenation to build a TSQL command):
DbParameter param = cmd.CreateParameter();
param.ParameterName = "@foo";
param.DbType = DbType.DateTime;
param.Value = yourDateTime; // the DateTime returned from .ParseExact
cmd.Parameters.Add(param);
or for a SqlCommand
:
cmd.Parameters.Add("@foo", SqlDbType.DateTime).Value = yourDateTime;
If you genuinely need a string, then just use the string directly as a [n][var]char parameter.
Also - in this case, to parse the date I would use the invariant culture (since culture doesn't feature in the format):
DateTime yourDateTime =
DateTime.ParseExact(dateString, "yyyyMMdd", CultureInfo.InvariantCulture);
From the conversation, it seems you might also need to go from a DateTime to a string, in which case simply reverse it:
string dateString = yourDateTime.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…