2012-08-17 122 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