I've never used the "appSettings" before. How do you configure this in C# to use with a SqlConnection, this is what I use for the "ConnectionStrings"
SqlConnection con = new SqlConnection(); con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
And this is what I have for the "appSettings"
SqlConnection con = new SqlConnection(); con = ConfigurationManager.AppSettings("ConnectionString");
but it is not working.
Gets the AppSettingsSection data for the current application's default configuration.
To access these values, there is one static class named ConfigurationManager, which has one getter property named AppSettings. We can just pass the key inside the AppSettings and get the desired value from AppSettings section, as shown below.
The ConfigurationManager class enables you to access machine, application, and user configuration information. This class replaces the ConfigurationSettings class, which is deprecated. For web applications, use the WebConfigurationManager class.
Your web.config
file should have this structure:
<configuration> <connectionStrings> <add name="MyConnectionString" connectionString="..." /> </connectionStrings> </configuration>
Then, to create a SQL connection using the connection string named MyConnectionString
:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
If you'd prefer to keep your connection strings in the AppSettings
section of your configuration file, it would look like this:
<configuration> <appSettings> <add key="MyConnectionString" value="..." /> </appSettings> </configuration>
And then your SqlConnection constructor would look like this:
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["MyConnectionString"]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With