[GIS] How to edit feature in SQLServer geodatabase

arcgis-10.2arcobjectsesri-geodatabasesql server

I am using ArcMap 10.2.1 and ArcObjects 10.2.1, I just want to connect to a geodatabase and edit, add, delete a feature from a feature class using ArcObjects, I tried to connect to database and it works fine but I can't start editing and adding a feature to it.

Here is my code:

public void open_Workspace()
    {
    Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SqlWorkspaceFactory");
    IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
    IWorkspaceFactory2 workspaceFactory2 = workspaceFactory as IWorkspaceFactory2;
    // Build a connection string.
    String[] connectionProps = 
{
    "dbclient=SQLServer", "serverinstance=CRAZYVIRUS-HP", 
        "database=land_gis", "authentication_mode=OSA" 
};
    String connString = String.Join(";", connectionProps);

    IWorkspace workspace = workspaceFactory2.OpenFromString(connString, 0);
    ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explict Cast
    ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("DBO.test");

    ESRI.ArcGIS.Carto.IFeatureLayer featureLayer = new ESRI.ArcGIS.Carto.FeatureLayerClass();

    featureLayer.FeatureClass = featureClass;
    featureLayer.Name = featureClass.AliasName;
    featureLayer.Visible = true;

    IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;
    IWorkspaceEdit2 workspaceEdit2 = (IWorkspaceEdit2)workspaceEdit;
    IMultiuserWorkspaceEdit muWorkspaceEdit = (IMultiuserWorkspaceEdit)workspace;

    Make sure that non-versioned editing is supported. If not, throw an exception.
   if (!muWorkspaceEdit.SupportsMultiuserEditSessionMode
       (esriMultiuserEditSessionMode.esriMESMNonVersioned))
   {
       throw new ArgumentException(
           "The workspace does not support non-versioned editing.");
   }

   // Start a non-versioned edit session.
   muWorkspaceEdit.StartMultiuserEditing
   (esriMultiuserEditSessionMode.esriMESMNonVersioned);

    featureClass.CreateFeature();


    // Stop the edit session. The saveEdits parameter indicates the edit session
    // will be committed.
    workspaceEdit.StopEditing(true);

}

When I run above code I got this error Unable to cast COM object of type 'System.__ComObject' to interface type 'ESRI.ArcGIS.Geodatabase.IWorkspaceEdit2'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{199A1F9D-435A-4118-9B3F-4E0B8F984AA0}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

I tried to enable versioning or disable it but there is not any chance to make it work… Can you help me find the solution into this error?
Is there any thing wrong with my connection?

Best Answer

I think @MichaelMiles-Stimson is right and you should use the SdeWorkspaceFactory instead of SqlWorkspaceFactory and then solve the following errors you mentioned.

The examples are here and this way works in our project.