繁体   English   中英

如何批量插入地理到新的SQL Server 2008表

[英]How to bulk insert geography into a new sql server 2008 table

我有一个非常大的形状文件,包含数十万行多边形和其他相关数据,如格式化寻址和APN编号。 如何在不使用Shape2SQL之类的东西的情况下将这些数据放入具有地理位置的表中? 我不能很好地为每一行永久运行一个insert语句,最佳解决方案是创建一个csv或一个格式正确的bin文件,然后执行批量插入,或bcp ,或openrowset,但尝试,尝试,尝试我可能无法获得csv文件或bin文件。 有人可以帮忙吗?

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

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();

但这只能让我单独导入多边形 - 我还需要其他所有东西。

经过几天的头痛,我得出的结论是没有答案。 即便是强大的ESRI也没有任何线索。 值得庆幸的是,我确实想出了一个不同的灵魂。 在我的表定义中,我创建了一个NVARCHAR(MAX)列来保存我的地理位置的WFT,并将WFT添加到我的csv文件中,然后在批量插入后运行一个表宽更新语句,将WFT转换为实际的地理类型。 还要调整csv文件以使用除a之外的其他字符,以便与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); }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM