SQLConnection asp.net Best Practices?

Discussion in 'ASP .Net' started by mid, Dec 13, 2004.

  1. mid

    mid Guest

    Hello!,

    What is the Best Practice about dealing with a sqlconnection while page
    executes?
    Let me explain:
    Supose you have several dropdownlists, datagrids, etc, in the same page,
    that need to retreive data from sql server. Right now I open/close a sql
    connection for each object that I want to populate, but I think this is not
    the good way...

    If I open the connection at page.load and close at page.unload and
    use/reuse that connection in all the subs that get executed while page...
    executes :), is this a good idea??

    How do you gurus deal with this question?

    Thanks
    mid
     
    mid, Dec 13, 2004
    #1
    1. Advertising

  2. Connection pooling takes care of connection reuse and the like. Open a
    connection at the last possible second and close it as soon as possible
    afterwards. Don't open at the beginning and close at the end.
     
    =?Utf-8?B?U2NvdHQgU2ltb25z?=, Dec 13, 2004
    #2
    1. Advertising

  3. Ideally, you shouldn't be using any database logic in your Page class.
    Instead, build a business class that does your database operations, and have
    that class do the work. The Page should only contain interface logic that
    uses the business class(es) to manipulate data, and displays the data in the
    browser interface. The business class should open and close Connections as
    quickly as possible.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Neither a follower
    nor a lender be.

    "mid" <> wrote in message
    news:1fb161hj17oho$...
    > Hello!,
    >
    > What is the Best Practice about dealing with a sqlconnection while page
    > executes?
    > Let me explain:
    > Supose you have several dropdownlists, datagrids, etc, in the same page,
    > that need to retreive data from sql server. Right now I open/close a sql
    > connection for each object that I want to populate, but I think this is

    not
    > the good way...
    >
    > If I open the connection at page.load and close at page.unload and
    > use/reuse that connection in all the subs that get executed while page...
    > executes :), is this a good idea??
    >
    > How do you gurus deal with this question?
    >
    > Thanks
    > mid
     
    Kevin Spencer, Dec 13, 2004
    #3
  4. mid

    mid Guest

    Thanks Scott,

    I am aware of connection pooling but thought it was helpfull when several
    users are executing the page (in different threads). Do you mean that when
    I open a connection to populate a dropdown then close it then open it again
    to populate another object there is no overhead? Behind the scenes the
    connection is still open?

    Where to declare the connection (not open)? At page Load?

    Thanks
    mid

    On Mon, 13 Dec 2004 12:01:01 -0800, Scott Simons wrote:

    > Connection pooling takes care of connection reuse and the like. Open a
    > connection at the last possible second and close it as soon as possible
    > afterwards. Don't open at the beginning and close at the end.
     
    mid, Dec 14, 2004
    #4
  5. mid

    Scott Allen Guest

    I'd keep the connection as a local variable inside of a method. If
    down the road you decide to use some common code as a data access
    layer this approach keeps things simple to convert.

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Tue, 14 Dec 2004 09:33:07 +0000, mid <>
    wrote:

    >Thanks Scott,
    >
    >I am aware of connection pooling but thought it was helpfull when several
    >users are executing the page (in different threads). Do you mean that when
    >I open a connection to populate a dropdown then close it then open it again
    >to populate another object there is no overhead? Behind the scenes the
    >connection is still open?
    >
    >Where to declare the connection (not open)? At page Load?
    >
    >Thanks
    >mid
    >
    >On Mon, 13 Dec 2004 12:01:01 -0800, Scott Simons wrote:
    >
    >> Connection pooling takes care of connection reuse and the like. Open a
    >> connection at the last possible second and close it as soon as possible
    >> afterwards. Don't open at the beginning and close at the end.
     
    Scott Allen, Dec 14, 2004
    #5
    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. karim
    Replies:
    0
    Views:
    474
    karim
    Jul 13, 2003
  2. Krissy
    Replies:
    2
    Views:
    4,882
  3. Natty Gur
    Replies:
    0
    Views:
    415
    Natty Gur
    Aug 26, 2003
  4. Ken Fine
    Replies:
    2
    Views:
    586
    Guest
    Jun 25, 2005
  5. John Dalberg
    Replies:
    3
    Views:
    583
    samuelhon
    Nov 16, 2006
Loading...

Share This Page