2011-08-23 102 views
3

我有一个非常大的形状文件,其中包含数十万行多边形和其他关联数据,如格式化的寻址和APN号码。如何将这些数据放入带有地理位置的表格中,而不使用Shape2SQL之类的东西?我不能很好地为每一行运行一个永久需要的插入语句,最佳的解决方案是创建一个csv或格式正确的bin文件,然后执行批量插入操作,或者打开一个批处理文件,但尝试尝试,试试我可能无法获得csv文件或bin文件。任何人都可以帮忙吗?如何将地理数据插入到新的sql server 2008表中

以下代码是我可以管理的最佳代码。

SqlGeographyBuilder sql_geography_builder = new SqlGeographyBuilder(); 
sql_geography_builder.SetSrid(4326); 
sql_geography_builder.BeginGeography(OpenGisGeographyType.Polygon); 
sql_geography_builder.BeginFigure(-84.576064, 39.414853); 
sql_geography_builder.AddLine(-84.576496, 39.414800); 
sql_geography_builder.AddLine(-84.576522, 39.414932); 
sql_geography_builder.AddLine(-84.576528, 39.414964); 
sql_geography_builder.AddLine(-84.576095, 39.415015); 
sql_geography_builder.AddLine(-84.576064, 39.414853); 
sql_geography_builder.EndFigure(); 
sql_geography_builder.EndGeography(); 
SqlGeography sql_geography = new SqlGeography(); 
sql_geography = sql_geography_builder.ConstructedGeography; 


FileStream file_stream = new FileStream("C:\\PROJECTS\\test.bin", FileMode.Create); 
BinaryWriter binary_writer = new BinaryWriter(file_stream); 

sql_geography.Write(binary_writer); 
binary_writer.Flush(); 

binary_writer.Close(); 
file_stream.Close(); 
file_stream.Dispose(); 

SqlConnection sql_connection = new SqlConnection(connection_string); 
sql_connection.Open(); 

SqlCommand sql_command = new SqlCommand(); 
sql_command.Connection = sql_connection; 
sql_command.CommandTimeout = 0; 
sql_command.CommandType = CommandType.Text; 
sql_command.CommandText = "INSERT INTO [SPATIAL_TEST].[dbo].[Table_1] ([geo]) " + 
       "SELECT [ors].* " + 
       "FROM OPENROWSET(BULK 'C:\\PROJECTS\\AMP\\test.bin', SINGLE_BLOB) AS [ors] "; 
sql_command.ExecuteNonQuery(); 

sql_command.Dispose(); 
sql_connection.Close(); 
sql_connection.Dispose(); 

但这只是让我单独导入多边形 - 我需要一切为好。

+0

CSV文件,只是普通的不工作的SSIS不会接受WFT或WFD作为批量插入一个有效的字段。我唯一的希望是创建一个像这里看到的二进制数据文件(http://stackoverflow.com/questions/282604/best-way-to-export-import-ms-sql-2008-geography-data)和上面的代码如果我想要做的只是输入地理位置,那么效果很好。问题是我有更多的信息进入文件,而不仅仅是地理位置,我不知道如何构建我的二进制文件或我的格式文件,所以我可以做一个适当的bcp或openrowset。 –

+0

对于未来的读者,值得一提的是.NET中的SqlBulkCopy可以直接插入SqlGeoegraphy类型。看到这个答案在这里:https://stackoverflow.com/a/21128445 –

回答

0

好几天头痛后,我得出的结论是没有答案。即使是强大的ESRI也没有任何线索。谢天谢地,我确实想出了一个不同的灵魂。在我的表格定义中,我创建了一个NVARCHAR(MAX)列来保存我的地理区域的WFT,并将该WFT添加到我的csv文件中,然后在批量插入后运行表格范围更新语句以将WFT转换为实际的地理类型。还可以调整csv文件使用一个不同的角色,除了一,用监守的WFT包含分离,的

SqlGeographyBuilder sql_geography_builder = new SqlGeographyBuilder(); 
sql_geography_builder.SetSrid(4326); 
sql_geography_builder.BeginGeography(OpenGisGeographyType.Polygon); 
sql_geography_builder.BeginFigure(-84.576064, 39.414853); 
sql_geography_builder.AddLine(-84.576496, 39.414800); 
sql_geography_builder.AddLine(-84.576522, 39.414932); 
sql_geography_builder.AddLine(-84.576528, 39.414964); 
sql_geography_builder.AddLine(-84.576095, 39.415015); 
sql_geography_builder.AddLine(-84.576064, 39.414853); 
sql_geography_builder.EndFigure(); 
sql_geography_builder.EndGeography(); 
SqlGeography sql_geography = new SqlGeography(); 
sql_geography = sql_geography_builder.ConstructedGeography; 

StreamWriter stream_writer = new StreamWriter("C:\\PROJECTS\\AMP\\test.csv"); 
stream_writer.AutoFlush = true; 
stream_writer.WriteLine("1?123 TEST AVE?" + sql_geography.ToString() + "?"); 
stream_writer.Flush(); 
stream_writer.WriteLine("2?456 TEST AVE?" + sql_geography.ToString() + "?"); 
stream_writer.Flush(); 
stream_writer.WriteLine("9?789 TEST AVE?" + sql_geography.ToString() + "?"); 
stream_writer.Flush(); 
stream_writer.Close(); 
stream_writer.Dispose(); 

SqlConnection sql_connection = new SqlConnection(STRING_SQL_CONNECTION); 
sql_connection.Open(); 

SqlCommand sql_command = new SqlCommand(); 
sql_command.Connection = sql_connection; 
sql_command.CommandTimeout = 0; 
sql_command.CommandType = CommandType.Text; 
sql_command.CommandText = "BULK INSERT [SPATIAL_TEST].[dbo].[Table_1] " + 
          "FROM 'C:\\PROJECTS\\AMP\\test.csv' " + 
          "WITH (FIELDTERMINATOR = '?', ROWTERMINATOR = '\n') " + 
          "" + 
          "UPDATE [SPATIAL_TEST].[dbo].[Table_1] " + 
          "SET [geo] = geography::STPolyFromText([geo_string], 4326) "; 
sql_command.ExecuteNonQuery(); 

sql_command.Dispose(); 
sql_connection.Close(); 
sql_connection.Dispose(); 

MessageBox.Show("DONE"); 
} 
catch (Exception ex) 
{ MessageBox.Show(ex.Message); } 
相关问题