website-database connectivity using web.config

website-database connectivity using web.config

Today, we will learn database connectivity using web.config <connectionStrings> property.

The main advantage of using connection string in web.config are:

  • Security– Web.config file is the configuration file which stores all the server setting required for the web application. And IIS server never allows to browse or view web.config file through browser. So it is very secure.
  • Usability– It is very bad habit and waste of time to use again and again connection string to connect the sql server. And use of inline connection string is also increases the line of code which indirectly increase the server load.

Here is the code of web.config file:

<connectionStrings>
<add name=”conn” connectionString=”Data Source=.\sqlexpress;Initial Catalog=mydatabase;Integrated Security=True”/>
</connectionStrings>

And in the c# code we have to use the following code:

SqlConnection Con=new SqlConnection(ConfigurationManager.ConnectionStrings[“conn”].ConnectionString);

In the web.config file in the <connectionStrings> property there is a property named <add> which holds the connection string. and a Name which will be used in the class file.

ConfigurationManager is used to get the properties of web.config file, and here we are using ConnectionStrings

property of web.config file.

See the example ConfigurationManager.ConnectionStrings[“conn”].ConnectionString

 

22 Replies to “website-database connectivity using web.config”

  1. seo

    Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?

  2. droping basemode

    I was curious iff you evver consifered changing thee structure of your blog?
    Its very well written; I love what youve got to say.
    But maybe yyou could a little more in the way of
    conhtent so people could connect with it better. Youve gott an awful lot of
    text for only having 1 or 2 pictures. Maybe you could space it out better?

  3. Lisette

    Howdy great blog! Does running a blog similar to this require a large
    amount of work? I have absolutely no knowledge of programming
    but I had been hoping to start my own blog in the near future.
    Anyhow, if you have any ideas or tips for new blog owners please share.
    I know this is off subject but I simply wanted to ask. Thanks
    a lot!

  4. online computers games

    Just wish to say your article is as astonishing.

    The clarity in your post is simply great and
    i can assumke you’re an expert on this subject. Finne with your permissio let me
    to grab your feed too keep up to date with forthcoming
    post. Thanks a million and please carry on the gratifying
    work.

  5. sleep apnea machine

    I am curious to find out what blog system you are working with?
    I’m experiencing some small security issues
    with my latest wegsite and I’d like to find something more risk-free.
    Do you have any suggestions?

Leave a Reply

Your email address will not be published. Required fields are marked *