Share us on:
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 .NET, There 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
We can add Connection String in the connectionStrings section.
In Connection String, there are some attribute
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>
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.
System.Configuration.
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