Save an OGR Layer coming from database


I am looking at an option to save OGR layer loaded from SQL Server to a local Shapefile.
Is there a way to achieve this? I tried following code but of no use

Shapefile shp = new Shapefile();
shp = axMap1.get_Shapefile(sqlOGRHandle);
shp.SaveAs(@“C: \Users\Umair\Desktop\a.shp”, null);

Please have a look at the example at AddLayerFromDatabase()

This wasn’t of much help but I have finally managed to fix it using following approach. Adding code for anyone who needs it :slight_smile:

    private void toolStripStartEdit_Click(object sender, EventArgs e)
        layer.OpenFromDatabase(CONNECTION_STRING, layerName, true);

        if (!layer.OpenFromDatabase(CONNECTION_STRING, layerName, true))
            MessageBox.Show("Failed to open layer: " + layer.get_ErrorMsg(layer.LastErrorCode));
        MessageBox.Show("Driver supports editing: " + layer.TestCapability(tkOgrLayerCapability.olcRandomWrite));
        if (!layer.get_SupportsEditing(tkOgrSaveType.ostSaveAll))
            MessageBox.Show("Can't edit a layer: " + layer.get_ErrorMsg(layer.LastErrorCode));
        axMap1.AddLayer(layer, true);
        _shpfile = layer.GetBuffer();
        _shpfile.InteractiveEditing = true;
        axMap1.CursorMode = tkCursorMode.cmEditShape;
        MessageBox.Show("Shapefile Loaded & Editing enabled");


    private void ToolStripStopAndSaveEditings_Click(object sender, EventArgs e)
        int count;
        var saveResults = layer.SaveChanges(out count);
        axMap1.CursorMode = tkCursorMode.cmPan;
        MessageBox.Show("Changes Saved Successfully");