I have a plugin that loads layers from SQL Server 2008. It has worked before but when I reset my PC, i wasn't able to backup my plugins. I am left with an older copy. Below is my python code to load the layers:

harvOps = "MSSQL:server=FSDSVR;database=AGDAT_FARMMAP_BIOPOWER;tables=dbo.V_HARV_OPS;trusted_connection=yes"
harvOpsLyr = QgsVectorLayer(harvOps,"Harvest States (Old)", "ogr" )

When i run the plugin in my new QGIS, it gives me this error in the Log Messages Panel under OGR tab:

2018-07-28T14:23:24 1   Data source is invalid (Unable to initialize connection to the server for MSSQL:server=FSDSVR;database=AGDAT_FARMMAP_BIOPOWER;tables=dbo.V_FARM_CUR;trusted_connection=yes,
            [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
            Try specifying the driver in the connection string from the list of available drivers:
            SQL Server
            SQL Server Native Client 10.0

Even this old version is already working previously. I have already checked my Data Sources (both 32 and 64-bit) and they all seem to be fine.

When i try to add layers from the QGIS interface, it works fine.

1 Answer 1


You need to add an ODBC connection outside of QGIS. Your windows environment is not recognizing the connection to MSSQL:server.

In control panel - click Administrative Tools -> Then ODBC connections based 64 or 32. Once ODBC connections launch then click drivers tab.

If SQL Server Native Client 10.0 is not there then you'll need to add the driver. If it is there, then you may not be administrator on your PC.

  • I have done that already since i was also using the driver to connect to SQL Server thru Excel. And again, i can connect to the database in QGIS when i try to add a spatial layer, no problems. I just cant connect programmatically. Aug 4, 2018 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.