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

Oğulcan Akca 196 Reputation points

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?

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
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

    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:

            <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" />
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points

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

    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.

    0 comments No comments