How can I write for 2 database connection string to app.config ?

Oğulcan Akca 196 Reputation points
2021-03-02T21:46:30.713+00:00

I am using 2 different databases in a project and I do not want to deal with sql codes. How do I configure connection string?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,535 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,104 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,511 questions
0 comments No comments
{count} votes

Accepted answer
  1. Dan Guzman 9,221 Reputation points
    2021-03-03T03:03:52.903+00:00

    An app.config file can have multiple connection strings. Each must be given a unique name so that your code can specify the the appropriate one for the query. For example:

    <configuration>
        <connectionStrings>
            <add name="Database1" connectionString="Data Source=Server1;Initial Catalog=Database1;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
            <add name="Database2" connectionString="Data Source=Server2;Initial Catalog=Database2;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
        </connectionStrings>
    </configuration>
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-03-03T07:14:32.047+00:00

    You also use ConfigurationManager to access the conectionstrings in the app.config.

    https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.connectionstrings?view=dotnet-plat-ext-5.0

    You should read the 'note:' about the deployment of the app.config and what you must understand and do for program deployment that uses an app.config.

    https://www.codeproject.com/Articles/6538/Configuration-Settings-File-for-providing-applicat

    0 comments No comments