首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SqlDbType与地理

SqlDbType与地理
EN

Stack Overflow用户
提问于 2010-09-17 23:24:43
回答 3查看 6.4K关注 0票数 9

当我的列是地理类型时,我应该使用什么SqlDbType枚举?我使用的是MS SQL Server2008 R2。

这就是我特别要找的:

代码语言:javascript
复制
// 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.????);
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-09-17 23:29:38

更新

试试这个:

代码语言:javascript
复制
//define parameter
command.Parameters.Add("@shape", SqlDbType.NVarChar);
//
//code in between omitted
//
//set value of parameter
command.Parameters["@shape"].Value = feature.Geometry.AsText();

摘自Inserting SQL 2008 Geometry With a SqlCommand

票数 0
EN

Stack Overflow用户

发布于 2010-10-19 16:10:16

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

代码语言:javascript
复制
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();
}

如果它是一个桌面应用程序,那么它就容易得多了。在SQL Geometry viewer的Code Project中有一个很好的例子,它对桌面或web都有帮助。

要直接使用SQLGeometry或SQLGeography,需要引用位于SQL Server Install/100/SDK/Assemblies中的Microsoft.SqlServer.Types.dll。

票数 13
EN

Stack Overflow用户

发布于 2019-10-18 02:02:20

当我尝试使用SqlDbType.NVarChar时,我收到错误消息

无法将参数值从地理位置转换为字符串

为我解决这个问题的是使用SqlDbType.Udt

代码语言:javascript
复制
var pgeo = cmd.Parameters.Add("@GeoLocation", SqlDbType.Udt);
pgeo.UdtTypeName = "Geography";

然后,在一个循环中,我设置了参数的值:

代码语言:javascript
复制
var ss = new SqlString(objValue.ToString());
var sc = new SqlChars(ss);
var geocode = SqlGeography.STPointFromText(sc, 4326);
cmd.Parameters["@GeoLocation"].Value = geocode;

注意:这需要Microsoft.SqlServer.TypesSystem.Data.SqlTypes

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3736666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档