# Shapefile to MSSQL with ogr2ogr fails to make connection

I am using ogr2ogr from OSGeo4W on my Windows 7 box to import a shapefile into a SQL Server database on one of my servers. I was able to successfully complete the conversion using shp2sqlserver, but I can't figure out why it fails when I try the same thing with ogr2ogr directly.

Here's the syntax of my ogr2ogr command:

ogr2ogr -f MSSQLSpatial "MSSQL:server=[server];database=[dbname];User Id=[uid];Password=[pwd]" C:\Path\to\file\shapefile.shp


When I run this, it fails first with an error saying "Cannot open database [dbname] requested by the login. The login failed." Next is another error that the "MSSQL Spatial driver doesn't currently support database creation. Please create database with the Microsoft SQL Server Client Tools."

I have checked and double-checked that the user id and password given works properly, that there is already a database with the name given (I substituted [dbname] here to speak in generalities), and that the database is owned by the user.

As mentioned, I was able to do this with shp2sqlserver so I don't think it's my database setup that is the problem. Is there a problem with my connection string? Is there some peculiarity within ogr2ogr that I'm not aware of?

I am using a Windows 7 machine connecting to SQL Server 2008 R2. Gdal is version 1.9.1.

-
The ODBC documentation suggests keywords UID and PWD instead of User ID and Password. –  klewis Oct 11 '12 at 21:21
Yeah. I tried that earlier but still no luck. –  spencerrecneps Oct 11 '12 at 21:34
Never mind. I substituted uid and pwd as you directed and it works now. I must have had some other error causing problems before I started messing with the uid and pwd designation. Thanks! –  spencerrecneps Oct 11 '12 at 21:39
Congrats on figuring out the problem. Since you did come up with an answer, it would be great if you would post it as an actual answer, possibly with a shoutout to @klewis, so that someone in a similar situation may be assisted. –  Get Spatial Oct 11 '12 at 23:21

The ODBC documentation suggests keywords UID and PWD instead of User ID and Password.

-
This also fixed my errors about not being a Trusted Connection... –  Tom Halladay Aug 28 '13 at 19:54

There is something unexpected I have come across, which is as given below:

COMMAND USED:

C:\Program Files\QGISDufour>ogr2ogr -overwrite -f “ESRI Shapefile” “D:\Abhinav-Task\temp\sqlexport.shp” ” MSSQL:server=WIN12;database=MAPS;tables=dbo.OGRExportTestTable;UID=RD; PWD=test1 ;”-sql “SELECT shapeid, shapename, shapegeom, bufferedshape.ToString(), bufferedshapearea area FROM OGRExportTestTable WHERE shapegeom.STGeometryType() = ‘POINT’”

ERROR MESSAGE:

FAILURE: Couldn’t fetch requested layer ‘SELECT shapeid, shapename, shapegeom, bufferedshape.ToString(), bufferedshapearea area FROM OGRExportTestTable WHERE shapegeom.STGeometryType() = ‘POINT”!

I don’t know why this has come up and to check the sql query, I have even run it in SQL MANAGEMENT STUDIO itself and everything is fine. But still somehow it throws up the above error in command line of OSGEO4W.

Will you please tell me how to get around this one??

-
@klewis if have any idea regarding my error, please do tell me –  abhinavgoyal02 Apr 25 at 11:15