3

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.

  • E_FAIL is a general error, which could mean many different things. Which line is it failing at? Also, if you are using a integrated security (operating system authentication), do you need to specify the username and password? – Evil Genius Jul 9 '14 at 16:46
  • Hi @EvilGenius thanks for your help,The error is in this line: return workspaceFactory.Open(connectionProps, 0); and I have entered user and password for OSA too.. – Majid Jul 9 '14 at 16:52
  • here is error code -2147467259 that I searched and it refferes to E_FAIL Unspecified failure 0x80004005 I think there is somthing wrong with my connection code..where can I find a working connection code that I compate it with mine? – Majid Jul 9 '14 at 17:03
  • Can you connect to that server from the same machine using ArcGIS? Have you initialized ArcObject properly? Have you tried using a different workspace type (ie. FileGDB) to test if you've got everything else working properly? There's a lot of other things that could be happening. – Evil Genius Jul 9 '14 at 17:07
  • Yes I have connected to that geodatabase using arcgis and also arcobject is initialized properly but I havent tried diffrent workspace..FileGDB reffers to a local geodatabase how can I use it to find probelam with my sql server – Majid Jul 9 '14 at 17:10
4

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.

  • I used your code and It does not trow any errors..But I added some codes to give me all feature datasets from DB and It does not do any thing..How do I simply find out that my connection is ok?..I also tried to use openfromfile using this code IWorkspace workspace = workspaceFactory2.OpenFromFile(@"C:\Users\CrazyVirus\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\Connection to CRAZYVIRUS-HP.sde",1); but it trows the old error..I get this file from arcmap 10.2.1 connection file..Do you suggest me to use SDE ? – Majid Jul 10 '14 at 8:42
  • Yes, I always use SDEWorkspacefactory. It is the generic and the recommended by ESRI. – Katah Jul 11 '14 at 9:24
  • Thanks a lot..Can you provide me a source that I can learn more about setting up and using it in ArcObjects? – Majid Jul 11 '14 at 11:41
  • I have another question about ARCSDE..Does I need to install it ob the server side?or it must be installed on client side? – Majid Jul 11 '14 at 12:38
  • Can I use the connection that you mentioned in case of add,update or delete features in a feature class?or I must use SDE? – Majid Jul 13 '14 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.