Save an OGR Layer coming from database

Hi,

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));
            return;
        }
        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));
            layer.Close();
            return;
        }
        //else
        //{
        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);
        layer.Close();
        axMap1.CursorMode = tkCursorMode.cmPan;
        MessageBox.Show("Changes Saved Successfully");
        axMap1.Refresh();
    }