22

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.

1
  • 1
    I would assume byte[]. Depends if you are using ADO.NET, Entity Framework, etc. Commented Apr 25, 2014 at 11:55

3 Answers 3

11

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();
        }
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

If you are using EntityFramework, then you should use DbGeography for your model properties which uses SqlGeography under the hood
@ChrisSchaller - that is already covered in other answers and there's still no indication in the question that the OP was using EF or not.
10

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

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

1 Comment

DbGeography is in the System.Data.Entity namespace, which is Entity Framework... I don't know that he's using that...
2

Isn't it the DbGeography class?

Namespace: System.Data.Spatial Assembly: System.Data.Entity (in System.Data.Entity.dll)

https://msdn.microsoft.com/en-us/library/system.data.spatial.dbgeography.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.