SqlConvert.SqlDate()

Apr 2, 2010 at 6:01 PM

hi, i'm using chinese version of the sql server 2008. when creating new task scheduler, the autorun will throw an error: Conversion failed when converting date and/or time from character string.

by default, the program will convert  datetime to "dd MMM yyyy HH:mm:ss"(03 Apr 2010 00:00:00), but in chinese environment, the sql server cannot convert this date format.

so, i fixed the code.

 

public static string SqlDate(DateTime value)
        {
            if ((value == DateTime.MinValue) || (value == DateTime.MaxValue))
                return "NULL";
            else
                return string.Format("'{0}'", value.ToString("dd MMM yyyy HH:mm:ss", ci.DateTimeFormat));
        }

==============>

public static string SqlDate(DateTime value)
        {
            if ((value == DateTime.MinValue) || (value == DateTime.MaxValue))
                return "NULL";
            else
                return string.Format("'{0}'", value.ToString("yyyy-MM-dd HH:mm:ss", ci.DateTimeFormat));
        }