0

I'm trying use the GDAL library in C++ to read a MSSQL spatial database file (*.mdf). I see there is a driver for MSSQL Spatial, but there is very limited documentation on how to connect and read from the database. I would like to read features from the database and tables in general. I am familiar with reading shapefiles and rasters, but not reading SQL databases.

I would guess you create a connection string, load the GDAL Driver, but then I'm not sure where to go from there. Perhaps execute a SQL query to get a layer. From the layer get all the features.

0

I would start by reading the pages for various database drivers, they do have different levels of examples and information so you might get more clues from comparing them.

http://www.gdal.org/drv_mssqlspatial.html

I just had a look at ODBC, and PostgreSQL but there are others like Oracle and more.

You'll want to start with a ogrinfo on the database itself, start with the minimum of the server and database name using the examples in the page above, something like this, but it depends on exactly how you refer to your specific database, and whether you need user/pass etc. Obviously we don't know what version of SQLSERVER you have so the string below won't be correct, for either the server or the database component:

ogrinfo "MSSQL:server=.\MSSQLSERVER2008;database=dbname;"

The "driver:datasourcename[layer]" pattern is common across GDAL vector sources when you aren't pointing to an actual file, or folder.

Once you've got that working you have a confidence that doing the same from C++ can work with a known source string and layer name and options, and you'll be delving into the API documentation and examples.

http://gdal.org/1.11/ogr/ogr_apitut.html

Other than that you'll need to provide details about what you've tried, and what you see. (It's easy to set up an ODBC data source on Windows for other drivers so that might be a way to make sure you are doing everything right in a simpler scenario. )

0

From @mdsumner tip and some trial and error, I was able to successfully connect. I attached my database .mdf file to SQLServer 2014 I'm running. I used this code to connect and report features in my feature table. Next up, need to figure out the ExecuteSQL command to get the other tables.

const char *pszDriverName = "MSSQLSpatial";
GDALDriver *poDriver;

string conStr = "MSSQL:server=<dbinstancename>;database=dbname;trusted_connection=true";

poDriver = GetGDALDriverManager()->GetDriverByName(pszDriverName);
if (poDriver == NULL)
{
    printf("%s driver not available.\n", pszDriverName);
    return;
}

GDALDataset *poDS;
poDS = (GDALDataset*)GDALOpenEx(conStr.c_str(), GDAL_OF_VECTOR, NULL, NULL, NULL);
if (poDS == NULL)
{
    printf("Open failed.\n");
    return;
}

// poDS contains all feature tables (not regular tables)
int layerCount = poDS->GetLayerCount();
OGRLayer *poLayer = poDS->GetLayerByName("dbo.name");

OGRFeature *poFeature;
...
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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