Can we declare variables in the 'app.config' file?
Asked Answered
P

4

13

I have a form which needs to get connected to SQL Server, and I have a drop down for selecting the list of databases and perform operations like primary key checking, etc.

But presently my connection string looks like this:

SqlConnection sConnection = new SqlConnection("Server=192.168.10.3;DataBase=GoalPlanNew;User Id=gp;Password=gp");

But apart from the given database, I need to take it variable, so that I can connect it to the database I select from the dropdown.

How can I do this?

Poltroonery answered 23/11, 2010 at 14:9 Comment(0)
N
37

Hmm you can declare your variables like this

<appSettings>
    <add key="SmtpServerHost" value="********" />
    <add key="SmtpServerPort" value="25" />
    <add key="SmtpServerUserName" value="******" />
    <add key="SmtpServerPassword" value="*****" />
</appSettings>

and read like

string smtpHost = ConfigurationManager.AppSettings["SmtpServerHost"];
int smtpPort = Convert.ToInt32(ConfigurationManager.AppSettings["SmtpServerHost"]);
Nance answered 23/11, 2010 at 14:16 Comment(7)
What would value contain here?Poltroonery
value contains your value :) If you write serverhost value "asd.abc.com", then your string smtpHost value will be "asd.abl.com"Nance
@SerkanHekimoglu You should probably also indicate namespace for ConfigurationManager - System.Configuration. And that appropriate reference should be added to the solution too.Monet
The only bits missing in this answer is that 'dont forget to add a reference to System.Configuration' should ve been mentioned.Gunboat
@TalhaImam 6 years passed after my answer. I already stopped coding .Net for a long time ago. But, as I remember, after typing ConfigurationManager, compiler was warning you to add related reference. All developers know that, something should be added as a reference while coding. So, it is not a missing mention.Nance
Not trying hard to disagree, but not everyone who comes here is a developer, many of them are developers-to-be. Its better to think about the newbies while answering, just what i think.Gunboat
@TalhaImam Do not worry. Compiler warns you about what to do, and what to add as a reference ;)Nance
V
3

I think he wants a "semi constant":

Web.Config

<?xml version='1.0' encoding='utf-8'?>
<configuration>
    <connectionStrings>
        <add name="YourName" providerName="System.Data.ProviderName" connectionString="Data Source={0}; Initial Catalog=myDataBase; User Id=myUsername; Password=myPassword;" />
    </connectionStrings>
</configuration>

CS file

String Servername = "Test";
String ConnectionString = String.Format(ConfigurationManager.ConnectionStrings["YourName"].ConnectionString, ServerName);
Vestigial answered 23/11, 2010 at 14:31 Comment(0)
A
1

you can use the connectionStrings tag in the app.config configuration. You can add as many as you want (giving them each a separate key) and then retrieve them

example app.config xml (set providerName to a valid provider, for example System.Data.SqlClient, and the appropriate connection string) :

<?xml version='1.0' encoding='utf-8'?>
  <configuration>
    <connectionStrings>
      <clear />
      <add name="firstDb" 
       providerName="System.Data.ProviderName" 
       connectionString="Valid Connection String;" />
      <add name="secondDb" 
       providerName="System.Data.ProviderName" 
       connectionString="Valid Connection String;" />
    </connectionStrings>
  </configuration>

example on getting them and listing them (in your case, you would create the appropriate items in the dropdown and set the values) :

ConnectionStringSettingsCollection settings =
            ConfigurationManager.ConnectionStrings;

        if (settings != null)
        {
            foreach(ConnectionStringSettings cs in settings)
            {
                Console.WriteLine(cs.Name);
                Console.WriteLine(cs.ProviderName);
                Console.WriteLine(cs.ConnectionString);
            }
        }
Adenoid answered 23/11, 2010 at 14:16 Comment(0)
F
0

You could use the AppSettings section. Read here for an example.

Formerly answered 23/11, 2010 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.