SqlDbType和Geography

Dis*_*ile 8 sql ado.net sql-server-2008

当我的列是Geography类型时,我应该使用什么SqlDbType枚举?我正在使用MS SQL Server 2008 R2.

这就是我正在寻找的具体内容:

// 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.????);
Run Code Online (Sandbox Code Playgroud)

Row*_*haw 12

SqlGeography 由SQL Server实现为CLR用户定义类型,因此您可以执行以下操作:

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" });
    command.ExecuteNonQuery();
}
Run Code Online (Sandbox Code Playgroud)

如果它是一个桌面应用程序,你会更容易.在SQL Geometry查看器的Code Project中有一个很好的例子,可以帮助桌面或Web.

您需要引用在SQL Server Install/100/SDK/Assemblies中找到的Microsoft.SqlServer.Types.dll才能直接使用SQLGeometry或SQLGeography.

  • 还有一个带有所需程序集的NuGet包:[Microsoft.SqlServer.Types(Spatial)](http://www.nuget.org/packages/Microsoft.SqlServer.Types/) (2认同)

Den*_*eev 0

更新

尝试这个:

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

摘自使用 SqlCommand 插入 SQL 2008 几何图形