Dynamics 365 Code - App.Config Dosyasından ConnectionString Verisini Okuma (To Read Connection Strings From App.Config)


Projemize System.Configuration referansını eklememiz gerekir.




    private String GetServiceConfiguration()
        {
            // Get available connection strings from app.config.
            int count = ConfigurationManager.ConnectionStrings.Count;
            // Create a filter list of connection strings so that we have a list of valid
            // connection strings for Microsoft Dynamics CRM only.
            List<KeyValuePair<String, String>> filteredConnectionStrings =
            new List<KeyValuePair<String, String>>();
            for (int a = 0; a < count; a++)
            {
                if (isValidConnectionString(ConfigurationManager.ConnectionStrings[a].ConnectionString))
                    filteredConnectionStrings.Add
                    (new KeyValuePair<string, string>
                    (ConfigurationManager.ConnectionStrings[a].Name,
                    ConfigurationManager.ConnectionStrings[a].ConnectionString));
            }
            // If one valid connection string is found, use that.
            if (filteredConnectionStrings.Count == 1)
            {
                return filteredConnectionStrings[0].Value;
            }
            return null;
        }

        private Boolean isValidConnectionString(String connectionString)
        {
            // At a minimum, a connection string must contain one of these arguments.
            if (connectionString.Contains("Url=") ||
            connectionString.Contains("Server=") ||
            connectionString.Contains("ServiceUri="))
                return true;
            return false;
        }

Hiç yorum yok:

Yorum Gönder