SQL Server Spatial Types – Using SQL Server’s Spatial Types in a .Net Application

netsql server

For an upcoming project, one of the requirements is to store and use basic geogrpahic data as part of an existing SQL-Server/.Net application.

It seems obvious that we could use the SQL Spatial types, however, there doesn't seem to be any way to (say) read them from the result of a SQL query, or to save an updated POI/polygon.

Do developers really have to handle the conversion themselves at the Stored procedure level, or is there a way of using the types natively?

Best Answer

Is this a desktop application or, say a Silverlight application? If it is web-based you have to jump through some hoops. You could create a view that exposes WKT and then parse the WKT client side into WPF / Silverlight geometries.

If it is a desktop application you've got it quite a bit easier. There is a good example at the Code Project of an SQL Geometry viewer that will help for both desktop or web.

You need to reference Microsoft.SqlServer.Types.dll, found at SQL Server Install/100/SDK/Assemblies to use SQLGeometry or SQLGeography directly.

Technologies like RIA don't understand these types but there is a work-around. Essentially you create a view that casts the geometry as a varbinary(max), use it in your OR mapper, RIA, LINQ, etc. and then convert it back on the client. There is a good tutorial here

Very late edit: Silverlight won't accept them either because the previously mentioned DLL's aren't compiled against Silverlight. Getting Silverlight compatible versions is high on many peoples wish list!