gdal icon indicating copy to clipboard operation
gdal copied to clipboard

SQL Server insert feature, GetFID is always -1

Open bad-ch opened this issue 3 years ago • 1 comments

Using GDAL/OGR 3.5 with C#, .Net 6

The goal is to get back a FID from the new inserted row (SQL Server)

The column with the FID has the name qgs_fid, is an integer with a primary key and identity = true (autoincremental)

        var dataSource = Ogr.Open(connection, 1);
        var layerCount = dataSource.GetLayerCount();

        for (int layerIndex = 0; layerIndex < layerCount; layerIndex++)
        {
            var layer = dataSource.GetLayerByIndex(layerIndex);

            if (layer.GetName() != layerName)
            {
                continue;
            }

            layer.StartTransaction();

            foreach (var featureBody in featureList.Bodies)
            {
                var newFeature = new Feature(layer.GetLayerDefn());
                UpdateFeature(ref newFeature, featureBody); //copy the attributes and geometry to the new feature
                var test = layer.CreateFeature(newFeature);

                var autoId = newFeature.GetFID();
            }

            layer.CommitTransaction();
            layer.Dispose();
        }

        dataSource.Dispose();

If have 2 cases which I have tested:

  1. use option Gdal.SetConfigOption("MSSQLSPATIAL_SHOW_FID_COLUMN","YES");

on line with the command: "layer.CreateFeature(newFeature);" I get the following exception: SQL Error SQLState=HY000, NativeError=0, Msg=[Microsoft][SQL Server Native Client 11.0]Not enough columns bound

  1. If I use my code without the option above, the row with the geometry is added, but if I try the get back the FID (newFeature.GetFID()) the result is always -1, also if i use the option Gdal.SetConfigOption("MSSQLSPATIAL_ALWAYS_OUTPUT_FID", "YES");

With the PostGIS connector I get back the new ID from the created feature.

bad-ch avatar Jul 07 '22 21:07 bad-ch

CC @szekerest

rouault avatar Jul 08 '22 09:07 rouault