** Bug in sqlformatter.cs **

Jun 22, 2010 at 10:48 AM

In function  : protected virtual void WriteValue(object value) the result can be wrong depending of the locale settings.

For example a number (double) with france locale settings can be written like this 19,6 but WriteValue do this 19,6.0.

This will cause SQL error.

So the solution is

        protected virtual void WriteValue(object value)
        {
            if (value == null)
            {
                this.Write("NULL");
            }
            else if (value.GetType().IsEnum)
            {
                this.Write(Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType())));
            }
            else
            {
                switch (Type.GetTypeCode(value.GetType()))
                {
                    case TypeCode.Boolean:
                        this.Write(((bool)value) ? 1 : 0);
                        break;
                    case TypeCode.String:
                        this.Write("'");
                        this.Write(value);
                        this.Write("'");
                        break;
                    case TypeCode.Object:
                        throw new NotSupportedException(string.Format("The constant for '{0}' is not supported", value));
                    case TypeCode.Single:
                    case TypeCode.Double:
// old code                       string str = value.ToString();
                        var _value = Convert.ToDouble(value);

                        string str = XmlConvert.ToString(_value);                     
                        if (!str.Contains('.'))
                        {
                            str += ".0";
                        }
                        this.Write(str);
                        break;
                    default:
                        this.Write(value);
                        break;
                }
            }
        }