none
How can i use a single connection in one web application. RRS feed

  • Question

  • HI Friends,

    How can i use a single connection in one web application. Right now i have to create a different connection for every data interchange with SQL. For example: For inserting a data i am creating a connection on button click event. And for fetching a record, i have to create a different connection for data grid view. Please suggest me a way where i just need to create a single connection and can use the same connection under complete application.

    Please guide me correctly. And if you have any small application that can resolve my problem, then please let me know..

    Thanks
    Wednesday, March 3, 2010 3:10 PM

Answers

  • If you are using .Net you will already have 'one' connection under the covers via the connection pooling.
    This is on by default. You may have several SqlConnection objects, but only one physical connection.

    "SQL Server Connection Pooling (ADO.NET)"
    http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx

    If you were to have one physical connection that is opened and shared by all code, then you may run into many issues.
    For example, if you have one open connection that executes a long running query, all other code paths will have to wait
    for it to complete before it will get it's turn.

    The recommendation is to open/execute/close with connection pooling.

    HTH
    //Michael
    This posting is provided "AS IS" with no warranties.
    Thursday, March 4, 2010 8:20 AM