Reputation: 25
I have 2 connection strings saved in Settings section of the app in Visual Studio.
How do I retrieve those fields in C# as I have only ever used VB and the below code:
My.Settings.*SettingName*
Seems that simple that its hard to find a clear answer for :)
Upvotes: 1
Views: 2210
Reputation: 3344
Visual Studio 2017
Code in Controller action (to fetch the settings)
var appsettings = System.Configuration.ConfigurationManager.AppSettings["EmailSupport"].ToString();
var consettings = System.Configuration.ConfigurationManager.ConnectionStrings["CRMDBContext"].ToString();
Web.config
Things are different for Visual Studio 2019, .net core
Upvotes: 2
Reputation: 3767
If you have set the Settings in Project->Properties…->Settings
, you can access them by Properties.Settings.Default.SettingName
.
For more info, you can refer to this answer:
https://stackoverflow.com/a/65175118/8335151
Upvotes: 0
Reputation: 5117
You could stored the connection string as a key in the configuration file as shown here.
Add a reference for System.Configuration.ConfigurationManager then read setting. In the following it's been done for Entity Framework but can work for conventional data access via a connection and command. You could also add in the data provider in your connection strings. Other than that depending on the project type you could also store information in appsettings.json.
All code below is here.
Note the constants are in this case done under project properties.
Upvotes: 1