Last Updated: November 23, 2017
· purekrome

Importing a Shapefile into Sql Server 2012

Assuming you have

1 - Some Shapefile.shp file (and related .prj files, etc).
2 - Sql Server 2012
3 - Ogr2Ogr2 installed (I've got this installed via the Quantum GIS app or via downloading the ogr2org.exe file from the some weird GIS website)

Lets import the shape file into a Sql Server 2012+ database.

Local database on your localhost or using a user thats on some windows domain:

"C:\<location of ogr2ogr>\ogr2ogr.exe" -overwrite -f MSSQLSpatial "MSSQL:server=<database server name>;database=<database name>;trusted_connection=yes" "C:\<path to shapefile>\<Shapefile>.shp"

Remote database (like on Azure, etc)

"C:\<location of ogr2ogr>\ogr2ogr.exe" -overwrite -f MSSQLSpatial "MSSQL:server=<database server name>;database=<database name>;Uid=<user name>;pwd=<supa seecwit password>;Driver={SQL Server Native Client 11.0}" "C:\<path to shapefile>\<Shapefile>.shp"

Here's a full example I just used....

"C:\Program Files (x86)\Quantum GIS Lisboa\bin\ogr2ogr" -overwrite -f MSSQLSpatial "MSSQL:server=tarantino;database=xwing;trusted_connection=yes" "C:\Temp\NY Neighbourhoods\Tribecca.shp"
c:\Temp\spatial cli tools\bin>"C:\Temp\spatial cli tools\bin\gdal\apps\ogr2ogr.exe" -overwrite -f MSSQLSpatial ";database=Shapefiles;Uid=pewpew;pwd=pewpew!!;Driver={SQL Server Native Client 11.0}" "C:\Temp\sa2-shp\SA2_2016_AUST.shp"

If you need to customize the sql driver connection string settings, refer to the connection string cheat sheet for any other settings you might need.