Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • ASP.NET : How to get connection string from Web.config?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 309
    Comment on it

     How to get connection string from Web.config?

    The information required for the communication between application and a particular database is provided by connection string. The connectionString is defined within connectionStrings element in the Web.config file. The connection string is initialized once and can be used in many web forms,hence whenever communication to different database is required by application, we only need to change the connectionString in Web.config file.

     

    The parameters of connectionString not only include driver name,database name,server name but can also include security information i.e username and password. Some of the parameters of connectionString are:

     

    • Server /Data Source : It specifies either name or network address of the instance of SQL Server to which connection is to be made. Specify local for server when connection to local computer is required.

     

    • Database/Initial Catalog : It specifies name of database to connect to.

     

    • Trusted_Connection/Integrated Security : The probable values of this parameter are true,false,yes,no and sspi(equivalent to true). The default value is false. When false, the username and password for SQL Server is to be specified but if true then the credentials of windows account are used for authentication.

     

    • Persist Security Info : The default value is false. When value is false and if the connection is open or was ever in open state then the sensitive information e.g password is not returned as connection part.

    Example of obtaining connection string from Web.config:

    In Web.config file the connectionStrings element which contains connectionString is defined as follows:

    <connectionStrings>
    <add name="DBConnectionString" connectionString="Server=(local);Database=StudentManagement System;Trusted_Connection=Yes;" />
    </connectionStrings>

    A connection named DBConnectionString is contained in connectionStrings element in root Web.config file. There are various other elements in web.config file therefore every element has children or parent. The connectionStrings element is direct child of <configuration> element.

    Accessing connectionString in connectionString.aspx.cs:

    using System;
    using System.Web;
    using System.Configuration;
    
    
    namespace connectionString
    {
        public partial class connectionString : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                var connectionstring = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
                    Response.Write(connectionstring);
                
            }
        }
    }
    

    Output:

    Server=(local);Database=StudentManagement System;Trusted_Connection=Yes;

    Explanation:

    ConfigurationManager is a class used to access information related to machine,application and user configuration. The ConfigurationManager class exposes the property ConnectionStrings which is used to get the actual connection string using the appropriate key(connection name). The System.Configuaration assembly must be included in the project to make use of ConfigurationManager class.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: