Azure Functions Database Connection String

Lereveme picture Lereveme · May 13, 2016 · Viewed 46.5k times · Source

How do I add or access an app.config file in Azure functions to add a database connection string?

If you're not supposed to add an app.config and there is a better way to access an external database to execute the function please let me know best practices. Thanks!

Answer

Todd DeLand picture Todd DeLand · Aug 7, 2017

Jan_V almost nailed it, which led me to experiment with this in the local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true;",
    "AzureWebJobsDashboard": ""
  },
  "ConnectionStrings": {
    "MyConnectionString": "[YourConnectionStringHere]"
  }
}

This allows you to use the ConfigurationManager.ConnectionStrings[] we are all used to.

var sqlConnection = ConfigurationManager
                   .ConnectionStrings["MyConnectionString"].ConnectionString;