Read DbConnection Info From AppSettings.json

Mitch McConnell 41 Reputation points
2021-09-07T03:11:10.473+00:00

In my controller, how can I read in the database connection string info from my appsettings.json file?

If my appsettings.json reads like this
"ConnectionStrings": {
"ContosoConnection": "Server=(localdb)\mssqllocaldb;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true"
},

How would I read it into this?

       [HttpPost]
        public async void RunMSSQLStoredProc()
        {
            using (SqlConnection con = new SqlConnection(this.Configuration.GetConnectionString("ContosoConnection")))
            {
                using (SqlCommand cmd = new SqlCommand("StoredProcMaster", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    con.Open();
                    await cmd.ExecuteNonQueryAsync();
                }
            }
        }
Developer technologies ASP.NET ASP.NET Core
Developer technologies C#
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Chao Deng-MSFT 801 Reputation points
    2021-09-07T06:50:09.6+00:00

    Hi @Mitch McConnell ,

    Reading Connection String from AppSettings.json file using IConfiguration interface.

    In the below example, the IConfiguration is injected in the Controller and assigned to the private property Configuration.

    Then inside the Controller, the Connection String is read from the AppSettings.json file using the GetConnectionString function.

    appsettings.json

    "ConnectionStrings": {  
         "ContosoConnection": "Server=(localdb)\\mssqllocaldb;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true"  
      }  
    

    Controller:

    public class TestController : Controller  
        {  
            private IConfiguration Configuration;  
      
            public TestController(IConfiguration _configuration)  
            {  
                Configuration = _configuration;  
            }  
            public IActionResult Index()  
            {  
                string connString = this.Configuration.GetConnectionString("ContosoConnection");  
                return View();  
            }  
        }  
    

    Result:
    Np8ib.png


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    ChaoDeng

    0 comments No comments

  2. Duane Arnold 3,216 Reputation points
    2021-09-07T09:57:05.137+00:00
    0 comments No comments

  3. Artemiy Moroz 271 Reputation points
    2021-09-07T17:12:35.42+00:00

    Not sure if this helps, but in C# console applications we can read Connection String from App.config file.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.