更改区域时DbGeography中的FormatException异常

时间:2014-10-17 21:39:37

标签: c# regional-settings

最简单的解决办法是什么?

当这段代码在澳大利亚(它写的地方)运行时,它可以工作。

public static void CreatePointTest()
{
  double lat = -33.613869;
  double lon = 153.123456;

  var text = string.Format("POINT({0} {1})", lon, lat);
  // 4326 is most common coordinate system used by GPS/Maps
  var ExceptionOnThisLine = DbGeography.PointFromText(text, 4326);
}

当我将计算机上的区域代码更改为"法语(法国)"并运行它,我在返回语句上得到一个异常,它似乎与数字格式相关:

System.Reflection.TargetInvocationException was unhandled  
_HResult=-2146232828   _message=Exception has been thrown by the target of an invocation.   HResult=-2146232828   IsTransient=false   Message=Exception has been thrown by the target of an invocation.   Source=mscorlib   StackTrace:
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
[snip...]
       InnerException: System.FormatException
       _HResult=-2146233033
       _message=24141: A number is expected at position 16 of the input. The input has ,613869.
       HResult=-2146233033
       IsTransient=false
       Message=24141: A number is expected at position 16 of the input. The input has ,613869.
       Source=Microsoft.SqlServer.Types
       StackTrace:
            at Microsoft.SqlServer.Types.WellKnownTextReader.RecognizeDouble()
            at Microsoft.SqlServer.Types.WellKnownTextReader.ParsePointText(Boolean parseParentheses)
            at Microsoft.SqlServer.Types.WellKnownTextReader.ParseTaggedText(OpenGisType type)
            at Microsoft.SqlServer.Types.WellKnownTextReader.Read(OpenGisType type, Int32 srid)
            at Microsoft.SqlServer.Types.SqlGeography.ParseText(OpenGisType type, SqlChars taggedText, Int32 srid)
            at Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, SqlChars taggedText, Int32 srid)
            at Microsoft.SqlServer.Types.SqlGeography.STPointFromText(SqlChars pointTaggedText, Int32 srid)
       InnerException:

1 个答案:

答案 0 :(得分:1)

替换你的行

var text = string.Format("POINT({0} {1})", lon, lat);

通过

var text = string.Format(System.Globalization.CultureInfo.InvariantCulture, "POINT({0} {1})", lon, lat);

在法语中,,用作十进制运算符而不是.