Connection String: Add Connection String in WebConfig in ASP.NET MVC

   12/15/2021 3:50:42 AM     Add Connection String in WebConfig Read Connection String from WebConfig ASP.NET MVC C# .NET ASP.NET     0

Share us on:

Linkedin GooglePlus

Hi, I am Shubham Kumar, I will describe how to add and read Connection String from WebConfig in ASP.NET MVC(.NET C#).

Connection String is important for reading or fetching data from the database, In .NETThere is a WebConfig file where we can add Connection String for connection between Front End(C#) to the backend(Database). 

 

Here, I will explain about Connection String

1. How to Add Connection String in WebConfig in ASP.NET MVC(.NET C#)

2. How to Read Connection String from WebConfig in ASP.NET MVC(.NET C#)

 

Here, I will Add Connection String in WebConfig

1. How To Add Connection String in WebConfig File 

We can add Connection String in the connectionStrings section.

 

Connection-String

 

In Connection String, there are some attribute 

  •  Name: You can add the name of the ConnectionString.
  • connectionString: here, you need to add Data Source(Server Name), Initial Catalog(Database Name),uid(userName), Password , Integrated Security(true or false),   
  •  providerName: You can add here providerName like Sql Provider(System.Data.SqlClient),etc.

Windows Authentication.


	<connectionStrings>		
		<add name="CMSConntectionString" connectionString="Data Source=DESKTOP-JAQ7AQ23\SQLEXPRESS; Initial Catalog=Employee; Integrated Security=true;" providerName="System.Data.SqlClient" />		
	</connectionStrings>

SQL Server Authentication.


<connectionStrings>		
		<add name="CMSConntectionString" connectionString="Data Source=DESKTOP-JAQ7AQ22\SQLEXPRESS; Initial Catalog=TechCMSdb1; Integrated Security=false;uid=sa; Password=master;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
	</connectionStrings>

2. How To Read Connection String from WebConfig in .NET(C#)

Here. I will show you how to read the connection string from the WebConfig file in C#.

if you want to read the connection string, you must be added "System.Configuration" Assembly to read Connection String value from the WebConfig file.

Add Reference.

 

Add-Reference

 

System.Configuration.

 

Add-Config

 

 


	using System.Configuration;

Read value Connection String from WebConfig

 


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

 

 

I hope this article will help you to learn, how to add and read Connection String from WebConfig file in .NET(C#). if you like this article please share Us on

Shubham Kumar

Shubham Kumar is a founder of AspNetBugs, .NET Team Leader. He has very good experience with designing and developing enterprise-scale applications. He is exprience in ASP.NET MVC,ASP.NET C#, ASP.NET Core, AngularJS, Web API, EPPlus, SQL, Entity Framework, JavaScript, jQuery, Kendo, Windows services etc.