Convert SQL geography to C#

前端 未结 1 346
有刺的猬
有刺的猬 2020-12-17 16:25

What is the C# equivalent of this geospatial T-SQL code?

DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText(\'POLYGON((-122.358 4         


        
相关标签:
1条回答
  • 2020-12-17 17:23

    Try this:

    public bool OneOffSTIntersect()
    {
        var g =
            Microsoft.SqlServer.Types.SqlGeography.STGeomFromText(
                new System.Data.SqlTypes.SqlChars(
                    "POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))"), 4326);
        // suffix "d" on literals below optional but explicit
        var h = Microsoft.SqlServer.Types.SqlGeography.Point(47.653d, -122.358d, 4326);
    
        // rough equivalent to SELECT
        System.Console.WriteLine(g.STIntersects(h));
    
        // Alternatively return from a C# method or property (get).
        return g.STIntersects(h);
    }
    

    MSDN's SqlGeography Methods page links to info on each of the C# equivalents to the critical calls in your T-SQL - e.g. STIntersects.

    0 讨论(0)
提交回复
热议问题