Get Multiple Connection Strings from appsettings.json

In .net core framework applications, appsettings are stored in json format unlike other web applications in asp.net (web forms or MVC) framework. You might require to have multiple connectionstrings in one json appsettings file in some live scenarios; here is how we can get multiple connection strings from appsettings.json.

using Microsoft.Extensions.Configuration;
using R1.DocumentMigrationToCloud.DAL;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq

 class Program
    {
        private static IConfiguration _iconfiguration;
        static void Main(string[] args)
        {
            Console.WriteLine("Hi From W3TechSchool Team");
            GetApplicationSettingsFile();
            
        }

        public Program(IConfiguration configuration)
        {
            _iconfiguration = configuration;
        }

        static void GetApplicationSettingsFile()
        {
            
            var builder = new ConfigurationBuilder()
                                 .SetBasePath(Directory.GetCurrentDirectory())
                                 .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
            _iconfiguration = builder.Build();



            var configValues = _iconfiguration.GetSection("ConnectionStrings").GetChildren().AsEnumerable().ToList();
            for (int i = 0; i < configValues.Count(); i++)
            {
                Console.WriteLine("Key : " + configValues[i].Key );
                Console.WriteLine("Values: " + configValues[i].Value);
            }
        }      
    }
appsettings.json file

{
  "ConnectionStrings": {
    "Default1": "Server=SERVER;Database=databse;User    Id=YOUR_USER;Password=PASSWORD;MultipleActiveResultSets=true",
    "Default2": "Server=SERVER;Database=databse;User    Id=YOUR_USER;Password=PASSWORD;MultipleActiveResultSets=true",
    "Default3": "Server=SERVER;Database=databse;User    Id=YOUR_USER;Password=PASSWORD;MultipleActiveResultSets=true",
    "Default4": "Server=SERVER;Database=databse;User    Id=YOUR_USER;Password=PASSWORD;MultipleActiveResultSets=true",
    "Defaul5": "Server=SERVER;Database=databse;User    Id=YOUR_USER;Password=PASSWORD;MultipleActiveResultSets=true",
    "Default6": "Server=SERVER;Database=databse;User    Id=YOUR_USER;Password=PASSWORD;MultipleActiveResultSets=true",
    "Default7": "Server=SERVER;Database=databse;User    Id=YOUR_USER;Password=PASSWORD;MultipleActiveResultSets=true",
    "Default8": "Server=SERVER;Database=databse;User    Id=YOUR_USER;Password=PASSWORD;MultipleActiveResultSets=true",
    "Default9": "Server=SERVER;Database=databse;User    Id=YOUR_USER;Password=PASSWORD;MultipleActiveResultSets=true",
    "Default10": "Server=SERVER;Database=databse;User    Id=YOUR_USER;Password=PASSWORD;MultipleActiveResultSets=true"
  }
}

Result in Console

I hope this code snippet sample will help you in getting multiple connection strings from appsettings.json in core framework applications.

Leave a Reply