在我看来,你的格式字符串是错误的。
我用来制作 SQL 日期和时间字符串的方法是:
/// <summary>
/// Formats a date time value so that it fits the JET specification
/// for date formats to be usable from within a SQL query.
/// The format is #MM/DD/YYYY HH:MM:SS# (including the hashs).
/// </summary>
public static string FormatJetDateAndTime(
DateTime dateToFormat)
{
return string.Format(
CultureInfo.CurrentCulture,
@"#{0:00}/{1:00}/{2} {3:00}:{4:00}:{5:00}#",
dateToFormat.Month,
dateToFormat.Day,
dateToFormat.Year,
dateToFormat.Hour,
dateToFormat.Minute,
dateToFormat.Second);
}
仅格式化日期,我使用这个:
/// <summary>
/// Formats the date portion of a date time value so that it fits into
/// the JET specification for date formats to be usable from within a SQL query.
/// The format is #MM/DD/YYYY# (including the hashs).
/// </summary>
public static string FormatJetDateOnly(
DateTime dateToFormat)
{
return string.Format(
CultureInfo.CurrentCulture,
@"#{0:00}/{1:00}/{2}#",
dateToFormat.Month,
dateToFormat.Day,
dateToFormat.Year);
}
为了格式化时间,我正在使用这个:
/// <summary>
/// Formats the time portion of a date time value so that it fits into
/// the JET specification for date formats to be usable from within a SQL query.
/// The format is #HH:MM:SS# (including the hashs).
/// </summary>
public static string FormatJetTimeOnly(
DateTime dateToFormat)
{
return string.Format(
CultureInfo.CurrentCulture,
@"#{0:00}:{1:00}:{2:00}#",
dateToFormat.Hour,
dateToFormat.Minute,
dateToFormat.Second);
}
当然,使用参数化查询作为Caius Jard points out 是最好的方法。