Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在sql中保存地理数据类型_C#_Sql_Asp.net_.net_Geography - Fatal编程技术网

C# 在sql中保存地理数据类型

C# 在sql中保存地理数据类型,c#,sql,asp.net,.net,geography,C#,Sql,Asp.net,.net,Geography,我正在尝试使用c asp向sql中的地理字段添加数据。净 当添加点或线串时,我没有得到错误,但当添加多边形时,我得到以下错误 Message: [DataConnection.HandleError]: Query: Proc_CSA_AssetUpdateLocation: caused exception: A .NET Framework error occurred during execution of user-defined routine or aggregate "geog

我正在尝试使用c asp向sql中的地理字段添加数据。净

当添加点或线串时,我没有得到错误,但当添加多边形时,我得到以下错误

  Message: [DataConnection.HandleError]: Query: Proc_CSA_AssetUpdateLocation: caused exception: A .NET Framework error occurred during execution of user-defined routine or aggregate "geography": 
System.ArgumentException: 24200: The specified input does not represent a valid geography instance.
System.ArgumentException: 
at Microsoft.SqlServer.Types.SqlGeography.ConstructGeographyFromUserInput(GeoData g, Int32 srid)
at Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, SqlChars taggedText, Int32 srid)
at Microsoft.SqlServer.Types.SqlGeography.Parse(SqlString s)
例如,我传递的数据是:

POLYGON((54.40854093377361 -6.197662353515625, 54.422126065167866 -6.212425231933594, 54.43011521616425 -6.164703369140625, 54.41093863702367 -6.128997802734375, 54.40094728183984 -6.150970458984375, 54.40854093377361 -6.197662353515625))
存储过程:

@Name nvarchar(20),
    @ModifiedWhen DateTime,
    @itemGUID UniqueIdentifier,
    @GeoLocs nvarchar(max),
    @Type int

    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    UPDATE dbo.csa_AssetGeoData 
    Set ItemModifiedWhen = @ModifiedWhen,GeoCord = @GeoLocs,GeographyTypeItemID = @Type, Name = @Name
    WHERE ItemGUID = @itemGUID
不幸的是,无论我看哪里,我都找不到数据中出现错误的原因

我的问题是,是什么导致了这个错误


如有必要,我可以提供更多信息,如果这太含糊而没有信息,我很抱歉。

您可以尝试传递字符串表示形式并在脚本中对其进行解析,如下所示:

SET @g = geometry::Parse('POLYGON((1 3, 1 3, 1 3, 1 3))');

我从这一页上抄下了这一行。

好的,我明白了。这与多边形线相互交叉时不同。不知道为什么,但它保存的是未交叉的和未交叉的。

根据这个博客,MSSQL是左撇子。有许多方法可以检查功能的有效性。这里有一个例子

        if (polygon.MakeValidIfInvalid().EnvelopeAngle() > 90)
        {

            region.Shape = polygon.ReorientObject().Serialize().Value;
        }
        else
        {
            region.Shape = polygon.Serialize().Value;
        }

序列化用于通过网络发送空间数据。因此,它将实例转换为可持久保存在数据库中的字节[]。

我尝试过,但这是几何图形而不是地理图形,因此会在sql中抛出错误;操作数类型冲突:sys.geometry与sys.geography不兼容实际上,通过将nvarcharmax值指定给地理类型的列,会导致解析失败并引发错误。这就是异常堆栈所说的。我也这么认为,但两者:点-6.261348724365234 53.351268586147086和线53.742213773433109-8.322143546875,53.02139221293762-8.080443359375,53.77468884583564-7.6190185546875,53.40298244814-6.8719482421875,54.0335863352108-7.2015380859375工作正常!