Select to view content in your preferred language

ArcGIS Pro SDK C#: Create a Database Connection

1592
3
01-27-2022 01:18 PM
JadedEarth
Frequent Contributor

I was just going to leave this as a minor thing, but it's getting really cumbersome.  When I use the geoprocessing tool, "CreateDatabaseConnection_management", to create an SDE connection in my Add-In app, the connected SDE geodatabase is populated with "randomly-assigned" tables, instead of the actual tables contained in the database. 

Running the tool in the Geoprocessing tab, however, the SDE database connection is populated with correct tables.  Is this a bug in the SDK?  Below is my code snippet:

 

 

//Create a new sde connection
try
{  
    ProgressDialog progDlg = new ProgressDialog("Creating Database Connection...", "Cancel", 100, true);
    progDlg.Show();

    //Execute a GP tool
    IReadOnlyCollection<string> args = Geoprocessing.MakeValueArray(workDir, connName, server, instance, "OPERATING_SYSTEM_AUTH", dbName, sdePath);
    IGPResult gp_result = await Geoprocessing.ExecuteToolAsync("CreateDatabaseConnection_management", Geoprocessing.MakeValueArray(args.ToArray()),
                                                null, new CancelableProgressorSource(progDlg).Progressor, GPExecuteToolFlags.Default);
    }
catch (Exception ex)
{
	ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Failed to create database connection: " + ex.Message, "Error Message", MessageBoxButton.OK, MessageBoxImage.Error);
}
0 Kudos
3 Replies
KimGarbade
Frequent Contributor

Are the "Randomly Assigned" tables not even contained in the database you are connecting to?  Can you send images of success (in geoprocessing window) and failure (using the add-in)?

0 Kudos
JadedEarth
Frequent Contributor

Hi,

Please the attached images.  As you can see, in my Add-In:

1. Users select the allowed database connection they can create.

2. Then they click the "Run" button.

3.  The SDE Connection is created in the working folder.  But if you expand the name, you'd see the tables are not the tables for the selected database.

4. As a workaround, I make the user click "Connection Properties" (image 4) and reselect the database.

5.  Then back at the Catalog pane, they click "Refresh" to show the actual tables for that database.

 

Why doesn't it just show the actual tables connected with the database?  The workaround is so cumbersome and some users may not follow the instructions I provided in the GUI.  Who wants to read those?

 

1_CreateSDE_Connection.JPG2_ClickRun.JPG

3_SDEConnection_created.JPG

4_Workaround_Conection_properties.JPG

5_ClickRefreshInCatalog.JPG

0 Kudos
KimGarbade
Frequent Contributor

Hmmm... so no matter what the user chooses (like if  they type or choose "GIS_TMP") they are initially connected to the "CEAP_Routed_Output" database instead... 

Here is a post that addresses a similar situation. 

https://community.esri.com/t5/python-questions/python-script-using-arcpy-createdatabaseconnection/td...

I think it is saying that you have to fully quality the inputs your putting in your MakeValueArray.  For example rather than populating the array with SQL_SERVER you would use database_platform="SQL_SERVER".  I think the post is suggesting that since the optional inputs into the CreateDatabaseConnection_management function are being left blank, python needs the fully qualified inputs to know which array values are used to populate which parameters of the function.  Sounds just crazy enough to work.

The only other thing I can think of is that when your using the function from the app it isn't overwriting a current connection file having the same name as the one you are creating; and that existing file is pointed at "CEAP_Routed_Output".  You might try setting overwriteoutput to true in your script or deleting any existing connection file with the same name in the same folder before creating the new one. 

 

0 Kudos