Get Connection String from App.Config

how to get connection string from app config in c#

try

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringNameFromWebConfig"].ConnectionString);

It should work

Get ConnectionString from app.config

It should be:

ConfigurationManager.ConnectionStrings["BO"].ConnectionString;

Edit:

You will need the corresponding libraries as well if you don't have them yet, as mentioned in the below answers I think its System.Configuration

So in full you should have:

public static string getNewConnection()
{
return ConfigurationManager.ConnectionStrings["BO"].ConnectionString;
}

How to retrieve connection strings from App.config

string constr = System.Configuration.ConfigurationManager.
ConnectionStrings["ConsoleApp1.Properties.Settings.abcdConnectionString"].ConnectionString;

If you change your app.config like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="abcdConnectionString"
connectionString="Data Source=xxx.yyy.org;Initial Catalog=MyCatalog;"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>

then

string constr = System.Configuration.ConfigurationManager.
ConnectionStrings["abcdConnectionString"].ConnectionString;

How do I get a list of all connection strings in app.config

Basically you don't really need the name of the Connection String but the connection itself. I believe you just need to go thru them with foreach and for each iteration you can get the connectionString till the last one in the app.config.

 foreach (System.Configuration.ConnectionStringSettings css in System.Configuration.ConfigurationManager.ConnectionStrings)
{
string connectionString = css.ConnectionString;
// encryption part
// rewriting the connectionString in the app.config or however you want ot be done
}

You mentioned you got the encryption part done and all you need is reading the strings.

Hope it helps!

Getting a ConnectionString from app.config

at runtime, there is just one config file. so the config file of the active project is only
considered. also, you cannot have a class library project as an active/startup project i.e.

say you have 4 Projects in your solution, and each of them has a config file, then when you run the application, only the active project's(the one which is your startup project) config file is recognized.

Now what can you do?

  1. if you just want to isolate the sections of the config file, then you can have config file in each of your Projects, which in turn, are referenced in the main projects config i.e.

    Web.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <appSettings file="YourSettings.config">
    <add key="KeyToOverride" value="Original" />
    <add key="KeyToNotOverride" value="Standard" />
    </appSettings>
    <system.web>
    <!-- standard web settings go here -->
    </system.web>

    YourSettings.config:

    <appSettings>
    <add key="KeyToOverride" value="Overridden" />
    <add key="KeyToBeAdded" value="EntirelyNew" />
    </appSettings>

    read more about it here

  2. if you want to have separate config files for your active project itself, than that's whole different story altogether.
    its kind of ugly tweak, but read about it here

how to read the connection string from App.config file by C#

Can you please try

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="dbConn" providerName="System.Data.SqlClient"
connectionString="Data Source=VANYA\SQLEXPRESS;Initial Catalog=mydatabase;User Id=sa;Password=123" />
</connectionStrings>
<appSettings>
<add key="dbConn" value="Data Source=VANYA\SQLEXPRESS;Initial Catalog=mydatabase;User Id=sa;Password=123" />
</appSettings>
</configuration>

Then use the second method.

Make sure you select the App.Config file in the solution explorer and in the property window select Copy to Output Directory to Copy Always. Now Build the application and try again.

See the screenshot



Related Topics



Leave a reply



Submit