Global Database connection

Discussion in 'ASP .Net' started by Spitfire, Aug 2, 2007.

  1. Spitfire

    Spitfire Guest

    Hi,

    I am trying to make a web application using C#. I need to access database in
    every webpage and for that I have created new database connection each time.
    This is not an efficient way at all.

    Is it possible to have some kind of Global database object which we can
    refer from any page? Or is there any alternate solution ?

    thanks your help
    Spitfire, Aug 2, 2007
    #1
    1. Advertising

  2. Actually it is very efficient, since ADO.NET makes use of the Connection Pool
    which is specifically designed to cache connection objects (up to 100 by
    default) and provide them on demand. So, best-practices coding dictates that
    you should create and open a new Connection object just before you do your
    database work, and then close it and allow it to return to the pool
    immediately afterward.

    in C#, the using ( ) { } statement construct
    ensures that Close or Dispose is called automatically once the closing brace
    is reached, even if an exception is thrown.
    -- Peter
    Recursion: see Recursion
    site: http://www.eggheadcafe.com
    unBlog: http://petesbloggerama.blogspot.com
    bogMetaFinder: http://www.blogmetafinder.com



    "Spitfire" wrote:

    > Hi,
    >
    > I am trying to make a web application using C#. I need to access database in
    > every webpage and for that I have created new database connection each time.
    > This is not an efficient way at all.
    >
    > Is it possible to have some kind of Global database object which we can
    > refer from any page? Or is there any alternate solution ?
    >
    > thanks your help
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Aug 2, 2007
    #2
    1. Advertising

  3. "Spitfire" <> wrote in message
    news:...

    > I am trying to make a web application using C#. I need to access database
    > in
    > every webpage and for that I have created new database connection each
    > time.
    > This is not an efficient way at all.


    Yes it is - it is *by far* the most efficient way you can do it.

    > Is it possible to have some kind of Global database object which we can
    > refer from any page?


    This is one of the worst things you can do in ASP.NET in terms of
    performance and scalability.

    ADO.NET brings you connection pooling, so you should create your connection
    at the very last moment and destroy it as soon as you no longer need it.


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Aug 2, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    1
    Views:
    756
    Alvin Bruney
    Jul 29, 2003
  2. Bryan
    Replies:
    2
    Views:
    4,454
    Karl Seguin
    Oct 4, 2004
  3. Colin Graham
    Replies:
    1
    Views:
    4,079
    Jeff Sheldon
    Apr 4, 2005
  4. Mythran
    Replies:
    5
    Views:
    4,923
    Mythran
    Oct 5, 2005
  5. Terry Jolly

    Making a database connection global

    Terry Jolly, May 12, 2006, in forum: ASP .Net
    Replies:
    35
    Views:
    1,036
    Damien
    May 15, 2006
Loading...

Share This Page