Click here to Skip to main content
15,867,756 members
Articles / Database Development / SQL Server / SQL Server 2008
Tip/Trick

TIP: Managing SqlGeography class from C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
4 Sep 2010CPOL 19.8K   4   1
How to Insert or Update an SqlGeography object in SQL Server 2008 from a C# application
It took me some time to get this to work. The problem is that SqlGeography C# type, which corresponds to Geography SQL type, is considered an user-defined type (UDT) by the .net SQL library.

So, the proper way to insert or update a Geography field is something like this:

C#
SqlGeography geo = // Get the coordinates from somewhere...

using (SqlCommand command = 
    new SqlCommand(@"UPDATE Points SET Point=@Point WHERE FeatureID='9999'", connection))
    command.Parameters.Add(new SqlParameter("@Point", geo) { UdtTypeName = "Geography" });
    command.ExecuteNonQuery();
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Peru Peru


Computer Electronics professional, Software Architect and senior Windows C++ and C# developer with experience in many other programming languages, platforms and application areas including communications, simulation systems, PACS/DICOM (radiology), GIS, 3D graphics and HTML5-based web applications.
Currently intensively working with Visual Studio and TFS.

Comments and Discussions

 
QuestionHow do you get access to SqlGeography? Pin
Rod at work19-Mar-18 6:25
Rod at work19-Mar-18 6:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.