什么是.net framework 4.0中的地理sql server数据类型的C#等价物?

时间:2021-12-25 16:59:35

net web application using .net 4.0 framework.

使用.net 4.0框架的网络Web应用程序。

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

我有一个存储过程,它接受sql server 2008 R2中的地理数据类型。

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

我想将C#代码中的数据插入SQL Server。

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

但是我无法找到我应该在C#中使用哪种数据类型,它等同于SQL Server 2008数据类型。

3 个解决方案

#1


2  

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

如果您使用的是实体框架,那么您搜索的数据类型是DbGeography。

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

以下是有关DbGeography对象 - >详细信息的更多信息。

#2


3  

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

这听起来很明显,但为什么不使用在SQL Server中作为UDT安装的相同数据类型--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:

以下适用于SQL Server 2012实例。我无法测试SQL Server 2008,但我认为它应该工作相同:

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

}

#3


1  

Isn't it the DbGeography class?

不是DbGeography类吗?

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

命名空间:System.Data.Spatial程序集:System.Data.Entity(在System.Data.Entity.dll中)

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

#1


2  

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

如果您使用的是实体框架,那么您搜索的数据类型是DbGeography。

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

以下是有关DbGeography对象 - >详细信息的更多信息。

#2


3  

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

这听起来很明显,但为什么不使用在SQL Server中作为UDT安装的相同数据类型--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:

以下适用于SQL Server 2012实例。我无法测试SQL Server 2008,但我认为它应该工作相同:

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

}

#3


1  

Isn't it the DbGeography class?

不是DbGeography类吗?

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

命名空间:System.Data.Spatial程序集:System.Data.Entity(在System.Data.Entity.dll中)

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