Best practice for establishing SDE Connection once in session for ArcGIS Pro Add-in?

474
1
Jump to solution
01-10-2023 09:53 AM
succip
by
New Contributor III

Hello, 

I'm developing an add-in with a range of tools and many of them access our SDE database (mostly for reading but sometimes writing). I've been told I should only access it once per session but I'm wondering if there's any examples on the best practice for this. 

Currently I've separated it out into a "Data" class, but my implementation feels overcomplicated because I want it to use a different connection string based on the development or production build. Does anyone have any guidance here? Any examples in the documentation?

Here's my current Data class:

 

 

public static class Data
    {
        private static DatabaseConnectionProperties _dbConnectionProperties = new DatabaseConnectionProperties(EnterpriseDatabaseType.Oracle)
        {
            AuthenticationMode = AuthenticationMode.DBMS,
            User = GetCredential("user"),
            Password = GetCredential("password"),
            Instance = GetCredential("instance")
        };
        public static DatabaseConnectionProperties dbConnectionProperties
        {
            get { return _dbConnectionProperties; }
        }

        private static Geodatabase _sdeGeodatabase = new Geodatabase(_dbConnectionProperties);
        public static Geodatabase SDEGeodatabase
        {
            get => _sdeGeodatabase;
        }

        private static string GetCredential(string credential)
        {
            DbConnectionStringBuilder dbConnectionStringBuilder = new DbConnectionStringBuilder();
            dbConnectionStringBuilder.ConnectionString = Settings.ConnectionString;
            if (credential == "user") return dbConnectionStringBuilder["User ID"].ToString();
            if (credential == "password") return dbConnectionStringBuilder["Password"].ToString();
            if (credential == "instance") return dbConnectionStringBuilder["Data Source"].ToString();
            return null;
        }
    }

 

 

Thanks in advance.

0 Kudos
1 Solution

Accepted Solutions
RichRuh
Esri Regular Contributor

I'm not sure I understand the question.  You could read the connection string from a registry key or configuration file or something like that (please don't store a password there 😀).

There's no problem connecting to a single geodatabase multiple times through the Pro SDK. The underlying code will share a single connection.

--Rich

View solution in original post

1 Reply
RichRuh
Esri Regular Contributor

I'm not sure I understand the question.  You could read the connection string from a registry key or configuration file or something like that (please don't store a password there 😀).

There's no problem connecting to a single geodatabase multiple times through the Pro SDK. The underlying code will share a single connection.

--Rich