Join Class MVC SQL Connection String

Let's learn how to work with database connection in mvc application, here in example I show you how to create connection string for SQL and connection string for Entity Framework

Create database connection in MVC

Now we learn how to connect SQL database using SQL connection string in MVC application

Open your web.config file in root of your web application, Now you can setup your database connection details in two ways.

Database connection string in Web.Config

1. Setup mvc web.config connection string sql server property directly in web.config with right providerName.

<add name="DefaultConnection" connectionString="Data Source=;Initial Catalog=WTRAppDatabase;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />

2. In web.config file appSettings section adding each property value then read the value and form the connection string in your code.

<add key="ServerName" value=""/>
<add key="DatabaseName" value="WTRAppDatabase"/>
<add key="SchemaName" value="dbo"/>
<add key="UserId" value="dbuser100"/>
<add key="Password" value="xxpassword123"/>
Read Database connection string in C#

Now read the database connection details in your C# code, create a utility class like below. Look how to create SQL server connection string in MVC

//namespace required
using System.Configuration;
using System.Text;
using System.Data.Entity;
using System.Data.EntityClient;
using System.Data.SqlClient;         

public static string DBConnectionString
    SqlConnectionStringBuilder providerCs = new SqlConnectionStringBuilder();
    providerCs.InitialCatalog = ConfigurationManager.AppSettings["DatabaseName"];
    providerCs.UserID = ConfigurationManager.AppSettings["UserId"];
    providerCs.Password = ConfigurationManager.AppSettings["Password"];
    providerCs.DataSource = ConfigurationManager.AppSettings["ServerName"];               

    providerCs.MultipleActiveResultSets = true;
    providerCs.TrustServerCertificate = false;
    providerCs.IntegratedSecurity = true;            

    return providerCs.ToString();

Note: in real life, you may need to keep encrypted username and password in web.config, in that case you simply decrypt the property value by using your encryption utility this way

providerCs.InitialCatalog = UtilityService.Decrypt(ConfigurationManager.AppSettings["DatabaseName"]);

Your connectionString is ready to be used in your class. MVC entityframework connection string

Now if you are using entityframework, then adding below code will make the job easier.

EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();
ecsb.Provider = "System.Data.SqlClient";
ecsb.ProviderConnectionString = providerCs.ToString();
ecsb.Metadata = string.Format("res://{0}/entityfilename.csdl|res://{0}/entityfilename.ssdl|
res://{0}/entityfilename.msl", typeof(Entities).Assembly.FullName);

Another way could be instead of keeping the connectionstring details in web.config file, keep them in a separate xml file and read the file using .net configuration framework

Note: If you are using Coe MVC then you should look at Asp.Net core database connection string example

Database connection string in MVC example
Aspnet MVC Training MVC tutorials, learn model view controllers with c#, develop database driven web application using MVC framework.
Hire .Net Developer
Free Tutorials
ASP.NET MVC Interview Questions Answers
Asp.Net MVC C# Examples | Join Asp.Net MVC Course | Core Tutorial