[GIS] Connecting to SQL Server Geodatabase using ArcObject in C#

arcobjectsesri-geodatabasesql server

I am going to save a polygon that is created from points to remote geodatabase. I am new with ArcObjects and can't understand how it works… at this point I want to open a connection to database using ArcObjects and save geometry in it. I am using this code:

public IWorkspace open_ArcSDE_Workspace(string server, string instance, string user,
string password, string database, string version)
{
    // Create the workspace factory.
    Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SqlWorkspaceFactory");
    IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
        (factoryType);

    // Create the connection properties.
    IPropertySet connectionProps = new PropertySetClass();
    connectionProps.SetProperty("dbclient", server);
    connectionProps.SetProperty("serverinstance", instance);
    connectionProps.SetProperty("authentication_mode", "OSA");//The type of authentication to use. Valid values are DBMS and operating systems authentication (OSA). DBMS is the default mode and is not required.
    connectionProps.SetProperty("user", user);
    connectionProps.SetProperty("password", password);

    // Open the workspace.
    //IWorkspace workspace = workspaceFactory.Open(connectionProps, 0);
    return workspaceFactory.Open(connectionProps, 0);
}

But it throws errors:

enter code hereError HRESULT E_FAIL has been returned from a call to a COM component.

Can you help me and give me some examples that I can understand… The only source I have found for ArcObjects is ESRI and I couldn't find ant solutions for it.

Best Answer

That should work without problems, double check if the parameters that you are passing to the IPropertySet object are right. Are you writing the "serverinstance" property properly? that's the most common problem.

        IPropertySet propertySet = new PropertySetClass();
        propertySet.SetProperty("dbclient", "SQLServer");
        propertySet.SetProperty("serverinstance", "myMachine\\myInstance");
        propertySet.SetProperty("database", "myDatabase"); // Only if it is needed
        propertySet.SetProperty("authentication_mode", "OSA")
        propertySet.SetProperty("user", "myUser");
        propertySet.SetProperty("password", "myPassword");

But also you can use that way:

    Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SqlWorkspaceFactory");
    IWorkspaceFactory2 workspaceFactory2 = (IWorkspaceFactory2)Activator.CreateInstance
(factoryType);

    // Build a connection string.
    String[] connectionProps = 
    {
        "dbclient=SQLServer", "serverinstance=MyMachine\\SqlExpress", 
            "database=MyDatabase", "authentication_mode=OSA" 
    };
    String connString = String.Join(";", connectionProps);

    IWorkspace workspace = workspaceFactory2.OpenFromString(connString, 0);

In the ArcObjects help the user and password are not included in the connection string, but probably you should added it.

But my question is: which ArcGIS version are you using? It seems that in 10.2 the SqlWorkspaceFactory is nomore supported, there is no online documentation yet and in the local help the chapter "Working with sql workspaces" is nowhere to be found.

Searching for the SqlWorkspaceFactory documentation in the local help, the description is subtle: The SDEWorkspaceFactory.Open method should be used to make connections to all databases and enterprise geodatabases.

If you just give a quick look at the documentation, you may missing that it suggest to use the SDE and not Sql one, without any explaination or anything else.

I hope this will help you.