Create Connection String in C#

SQL Server Connection String for Windows Authentication in App.Config file:

For Windows Authentication in the App, SQL Server Connection String.Config files have the following properties and are specified as follows.

Data Source: The SQL Server’s and its instance’s names.

Initial Catalogue: This is the database’s name.

Security Integration: By default, False. If it is set to true, Windows Authentication must be utilised.

<configuration>
    <connectionStrings>
        <add name="ConString" connectionString="Data Source=Mudassar-PC\SQL2005;Initial Catalog=Northwind;Integrated Security=true" />
    </connectionStrings>
</configuration>

SQL Server Connection String for SQL Server Authentication in App.Config file:

Connection string for SQL Server used in the app for SQL Server authentication.Config files have the following properties and are specified as follows.

Data Source: The SQL Server’s and its instance’s names.

Initial Catalogue: This is the database’s name.

User Id: The SQL Server’s User Id.

Password – The SQL Server password.

<connectionStrings>
    <add name="ConString" connectionString="Data Source=Mudassar-PC\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass1234"/>
</connectionStrings>

System addition.Configuration citation:

The very first thing you must do is add reference to the System in order to access the SQL Server Connection String and SQL Server Authentication from the App.Config file in code behind.the following Assembly configuration to the project.
1. From the context menu, select Add Reference by performing a right-click on the project.

2. Select the.Net Tab from the Add Reference dialogue box, then search for System.Configuration assembly. Simply choose it and click OK once you’ve located it.
3. The reference will appear in the Solution Explorer’s References folder once it has been inserted.

NameSpace:

You must import the aforementioned namespace.

using System.Configuration;

Using C#, reading the Connection String from the App.Config file:

Following the addition of the reference, you may use the App.Config file to read the SQL Server Connection String for Windows Authentication and SQL Server Authentication.

string connectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories