Friday 4 August 2017

How to specify connection string in web configuration file(web.config) in ASP.Net ?

As you know all the configuration settings are present into the web configuration file .
In this file we are learning about how to add connection string into the web.config file.

open the web config file by default the code is written in it is like that

<?xml version="1.0"?>

<configuration>

    <system.web>
      <compilation debug="true" targetFramework="4.5.2" />
      <httpRuntime targetFramework="4.5.2" />
    </system.web>

</configuration>

In the above code we have to add connection string for the connection to the database for save our data into the database.

So add following lines into it.

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

    <system.web>
      <compilation debug="true" targetFramework="4.5.2" />
      <httpRuntime targetFramework="4.5.2" />
    </system.web>
  <connectionStrings>
    <add name="DBCS" connectionString="Data Source=PC;Initial Catalog=Example;Integrated Security=true"
          providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

Add the above marked lines into the web.config file.
DBCS ==> NAme of connection string
Data Source= => Name of server (PC is my computer name it can be a IP addreess like 154.21.145.12)
Initial Catalog ==> It is the database name
Integrated Security=true ====> Is is for windows authentication

for Sql Server Authentication repalce it with

User Id=xyz; password=******
The complete description is shown into the following diagrams.


No comments:

Post a Comment

Creating First WEB API Project from scratch ?

  For creating Web API project open your Visual Studio Editor and Create New Project as shown in below figure. Now you have to select  ASP.N...