Search code examples
.netconnectionapp-config

.net 3.5: To read connectionstring from app.config?


How to read connection string info from app.config file using .net api?

Platform is .net 3.5

     <?xml version="1.0" encoding="utf-8" ?>
        <configuration>
            <connectionStrings>
                 <add connectionString="" providerName="" name=""/>
            </connectionStrings>
        </configuration> 

Solution

  • Please see Reading Connection Strings in Web.Config and App.Config and Enterprise Library DAAB Settings (on the Wayback Machine as the original got deleted)

    ConnectionStringSettings connection = ConfigurationManager.ConnectionStrings["MyConnectionString"]
    string connectionString = connection.ConnectionString
    

    You may need to add an assembly reference to System.Configuration