Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is C# equivalent of geography sql server datatype in .net framework 4.0?

net web application using .net 4.0 framework.

I have a Stored Procedure which accepts geography datatype in sql server 2008 R2.

I want to insert data from C# code into SQL Server.

But I'm not able to find which datatype I should use in C# that is equivalent to SQL Server 2008 datatype.

like image 864
Brijraj Avatar asked Apr 25 '14 11:04

Brijraj


People also ask

What is meant by C?

noun, plural C's or Cs, c's or cs. the third letter of the English alphabet, a consonant. any spoken sound represented by the letter C or c, as in cat, race, or circle. something having the shape of a C. a written or printed representation of the letter C or c.

What is C and why it is used?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C and C++ difference?

C is a function driven language because C is a procedural programming language. C++ is an object driven language because it is an object oriented programming. Function and operator overloading is not supported in C. Function and operator overloading is supported by C++.

What is C language with example?

C language is a system programming language because it can be used to do low-level programming (for example driver and kernel). It is generally used to create hardware devices, OS, drivers, kernels, etc. For example, Linux kernel is written in C. It can't be used for internet programming like Java, .Net, PHP, etc.


2 Answers

It may sound obvious, but why not use the same data type that has been installed as a UDT in SQL Server - SqlGeography?

The following works fine against a SQL Server 2012 instance. I'm unable to test against SQL Server 2008 but I'd assume it should work the same:

using System;
using Microsoft.SqlServer.Types;
using System.Data.SqlClient;
using System.Data;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main()
        {
            var geom1 = SqlGeography.STGeomFromText(
                        new System.Data.SqlTypes.SqlChars(
                        "LINESTRING(-122.360 47.656, -122.343 47.656)"), 4326);
            var geom2 = SqlGeography.STGeomFromText(
                        new System.Data.SqlTypes.SqlChars(
                        "LINESTRING(-100.0 45.0, -1420 49.0)"), 4326);
            using(var conn = new SqlConnection(
                  @"Server=Server;Database=master;Integrated Security=SSPI;"))
            {
                using (var cmd = new SqlCommand(
                    "select @parm1.STIntersects(@parm2)", conn))
                {
                    var p1 = cmd.Parameters.Add("@parm1", SqlDbType.Udt);
                    p1.UdtTypeName = "geography";
                    p1.Value = geom1;
                    var p2 = cmd.Parameters.Add("@parm2", SqlDbType.Udt);
                    p2.UdtTypeName = "geography";
                    p2.Value = geom2;
                    conn.Open();
                    Console.WriteLine(cmd.ExecuteScalar());
                }
            }
            Console.ReadLine();
        }
    }

}
like image 149
Damien_The_Unbeliever Avatar answered Oct 05 '22 23:10

Damien_The_Unbeliever


If you are using entity-framework then the datatype you search is DbGeography.

Here is some more information about DbGeography object -> Details.

like image 38
Misha Zaslavsky Avatar answered Oct 05 '22 23:10

Misha Zaslavsky