2012-08-17 123 views
-1

我写在C#在这里我使用ADO.NET连接到我的SQL Server 2012数据库的项目。我的数据库(除其他外)有两列。第一列表示一点的纬度,第二列表示经度。我使用一个程序来检查地图上的两个矩形是否截取了另一个矩形。列回复1个返回值是0或1 编辑:在这个例子中,我只用数字运行它,我计划添加变数后 如果我对SQL查询运行它我的程序工作正常。但是,如果我运行使用ADO.netmakeValid()错误SQL地理空间

如SQL查询过程删除错误是:

DECLARE @g geography; 
DECLARE @h geography; 
DECLARE @s geography; 

SET @g = geography::STGeomFromText('POLYGON((39.692 23.483, 23.483 39.671, 24.095 39.493, 23.466 39.800,39.692 23.483))', 4326); 

SET @h = geography::STGeomFromText('POLYGON((39.800096 23.296509, 39.628961 23.128967,39.43195 23.510742 ,39.7093 23.859558,39.800096 23.296509))', 4326); 
SET @h [email protected](); 
SET @g = @g.MakeValid(); 
SELECT @g.STIntersects(@h) as reply 

/* ===================== ================================================== =======================================

// ----- ------------------- AS ADO.NET程序是:------------------------ -------

SqlDataReader rdr = null; 
SqlConnection conn = new SqlConnection("Data Source=AGIS-PC;Initial Catalog=ship_maps;Integrated Security=True");// create a connection object 


String commandString = @"DECLARE @g geography; 
DECLARE @h geography; 
DECLARE @s geography; 
SET @h [email protected].MakeValid(); 
SET @g = @g.MakeValid(); 
SET @g = geography::STGeomFromText('POLYGON((39.692 23.483, 23.483 39.671, 24.095 39.493, 23.466 39.800,39.692 23.483))', 4326); 
SET @h = geography::STGeomFromText('POLYGON((39.800096 23.296509, 39.628961 23.128967,39.43195 23.510742 ,39.7093 23.859558,39.800096 23.296509))', 4326); 
SET @h [email protected](); 
SET @g = @g.MakeValid(); 
SELECT @g.STIntersects(@h) as reply"; 
SqlCommand cmd = new SqlCommand(commandString, conn); 

try 
{ 
// open the connection 
conn.Open(); 
// 1. get an instance of the SqlDataReader 
rdr = cmd.ExecuteReader(); 

while (rdr.Read()) 
{ 
// get the results of each column 
string vessel_name = (string)rdr["reply"]; 
TextBox1.Text += " " + vessel_name; 

// ..... 
}//while 
} 
finally 
{ 
//........... 
} 

// ================================== ======================= 他错误的东西与makevalid()我猜。我在SQL初始查询过类似的错误,然后我插入makevalid()和它的工作。 编辑:它发生在RDR = cmd.ExecuteReader();

Stack Trace: 


[SqlException (0x80131904): A .NET Framework error occurred during execution of user-defined routine or aggregate "geography": 
System.ArgumentException: 24200: The specified input does not represent a valid geography instance. Use MakeValid to convert the instance to a valid instance. Note that MakeValid may cause the points of a spatial instance to shift slightly. 
System.ArgumentException: 
at Microsoft.SqlServer.Types.SqlGeography..ctor(GeoData g, Int32 srid) 
at Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, SqlChars taggedText, Int32 srid) 
. 
A .NET Framework error occurred during execution of user-defined routine or aggregate "geography": 
Microsoft.SqlServer.Types.GLArgumentException: 24205: The specified input does not represent a valid geography instance because it exceeds a single hemisphere. Each geography instance must fit inside a single hemisphere. A common reason for this error is that a polygon has the wrong ring orientation. To create a larger than hemisphere geography instance, upgrade the version of SQL Server and change the database compatibility level to at least 110. 
Microsoft.SqlServer.Types.GLArgumentException: 
at Microsoft.SqlServer.Types.GLNativeMethods.GeodeticIsValid(GeoData& g, Double eccentricity, Boolean forceKatmai) 
at Microsoft.SqlServer.Types.SqlGeography.IsValidExpensive(Boolean forceKatmai) 
at Microsoft.SqlServer.Types.SqlGeography..ctor(GeoData g, Int32 srid) 
at Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, SqlChars taggedText, Int32 srid) 
.] 
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2084358 
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5096328 
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234 
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2294 
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33 
System.Data.SqlClient.SqlDataReader.get_MetaData() +86 
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +311 
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987 
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162 
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32 
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141 
System.Data.SqlClient.SqlCommand.ExecuteReader() +89 
gmaps.Button11_Click(Object sender, EventArgs e) +185 
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118 
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112 
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563 

请指教,或者如果这是另一个话题告诉我。我与SQL初学者

回答

0

有你的代码中的许多问题....

1)你呼吁@g和@h MakeValid()时,他们未初始化变量:

DECLARE @g geography; 
DECLARE @h geography; 

SET @h = @h.MakeValid(); 
SET @g = @g.MakeValid(); 

2.)什么是@s for?它从未使用过......

3.)@g是一个无效的自交多边形。适当的有效实例,以表示这是点集MULTIPOLYGON(((23.483 39.671,39.692 23.483,23.721 39.602,23.483 39.671)),((23.466 39.8,23.721 39.602,24.095 39.493,23.466 39.8)))

4.) @h具有不正确的环形方向(也就是说,假设您未尝试创建覆盖地球表面99%的多边形)。它应该是POLYGON((39.43195 23.510742,39.628961 23.128967,39.800096 23.296509,39.7093 23.859558,39.43195 23.510742))

所以,你的SQL查询应该是:

DECLARE @g geography; 
DECLARE @h geography; 
SET @g = geography::STGeomFromText('MULTIPOLYGON (((23.483 39.671, 39.692 23.483, 23.721 39.602, 23.483 39.671)), ((23.466 39.8, 23.721 39.602, 24.095 39.493, 23.466 39.8)))', 4326); 
SET @h = geography::STGeomFromText('POLYGON((39.43195 23.510742, 39.628961 23.128967, 39.800096 23.296509, 39.7093 23.859558, 39.43195 23.510742))', 4326); 
SELECT @g.STIntersects(@h) as reply; 

其中给出的结果 '1',如在SQL Server空间表示结果显示如下选项卡:

enter image description here

Pro Spatial with SQL Server 2012