SqlDbType and Geography

geography data type in c#
c# spatial library
spatial data entity framework
entityframework spatial
microsoft spatial example
net core geometry
system spatial .net core

What SqlDbType enumeration should I use when my column is the Geography type? I'm using MS SQL Server 2008 R2.

This is what I'm looking for specifically:

// ADO.net - what do I use for the SqlDbType when it's defined 
// as Geography in the stored proc
SqlCommand command = new SqlCommand();
command.CommandText = "dbo.up_Foobar_Insert";
command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add("@SomeGeographyType", SqlDbType.????);


Try this:

//define parameter
command.Parameters.Add("@shape", SqlDbType.NVarChar);
//code in between omitted
//set value of parameter
command.Parameters["@shape"].Value = feature.Geometry.AsText();

Taken from Inserting SQL 2008 Geometry With a SqlCommand

SqlDbType and geography, Update. Try the following: //define parameter command.Parameters.Add("@​shape", SqlDbType.NVarChar); // //code in between omitted // //set  1 You cannot set the DbType property of a SqlParameter to SqlDbType.Date. 2 Use a specific typed accessor if you know the underlying type of the sql_variant. SQL Server documentation. For more information about SQL Server data types, see Data types (Transact-SQL). See also. SQL Server Data Types and ADO.NET; SQL Server Binary and Large-Value Data

SqlGeography is implemented as a CLR user defined type by SQL Server, so you can do something a little like:

SqlGeography geo = // Get the geography from somewhere...

using (SqlCommand command = 
    new SqlCommand(@"dbo.up_Foobar_Insert", connection))
    command.Parameters.Add(new SqlParameter("@Point", geo) { UdtTypeName = "Geography" });

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.

Insert Geography data into SQL Server 2008, AddWithValue("@name", nameParameter); SqlParamater extents = new SqlParameter("@extents", SqlDbType.Udt); extents.UdtTypeName = "Geography"​  Registering the geography Type. The geography type is predefined and available in each database. You can create table columns of type geography and operate on geography data in the same manner as you would use other system-supplied types. Can be used in persisted and non-persisted computed columns. Examples A. Showing how to add and query

When I tried to use SqlDbType.NVarChar I received and error

Failed to convert parameter value from Geography to a String

What resolved this problem for me was to use SqlDbType.Udt

var pgeo = cmd.Parameters.Add("@GeoLocation", SqlDbType.Udt);
pgeo.UdtTypeName = "Geography";

And then later, in a loop, I set the value of the parameter:

var ss = new SqlString(objValue.ToString());
var sc = new SqlChars(ss);
var geocode = SqlGeography.STPointFromText(sc, 4326);
cmd.Parameters["@GeoLocation"].Value = geocode;

Note: this requires Microsoft.SqlServer.Types and System.Data.SqlTypes

Passing geography parameter to Stored Procedure - MSDN, What is the correct syntax to pass a geography parameter to a stored procedure using plain ADO.NET? I haven't SqlDbType.Udt(A SQL  When setting command parameters, the SqlDbType and DbType are linked. Therefore, setting the DbType changes the SqlDbType to a supporting SqlDbType. Applies to See also. Configuring Parameters and Parameter Data Types (ADO.NET) Data Type Mappings; SQL Server Data Types and ADO.NET

Accessing Data with Microsoft® . Net Framework 4, To insert a Geography object, you must add a reference to the Microsoft.​SqlServer.Types.dll assembly. If you have SQL SqlDbType.Udt) loc.​UdtTypeName  Can someone guide me to or give me an example on how would one go about insert/query Geography data in Sql Server 2008 ( Polygon with 4 points and Point in specific) using C# EF or Linq2Sql.

MCTS Self-Paced Training Kit (Exam 70-516): Accessing Data with , The followingcode adds three ZIPor postal codes with the corresponding Geography objects. Notice the magic number SqlDbType.Udt) loc.​UdtTypeName  Specifies the data type of a field, a property, or a Parameter object of a .NET Framework data provider.

Metadata exception on the Geography data type colunm, at Microsoft.SqlServer.Server.SqlMetaData..ctor(String name, SqlDbType dbType​, Int64 maxLength, Byte precision, Byte scale, Int64 localeId,  Gets or sets a string that represents a user-defined type as a parameter.