Keeping connection open: Good Idea?

Making the transition from ASP.Net to windows forms.  

While I would never do this in ASP.Net, it seems that it may not be a bad idea in windows forms: I have a pretty straightforward little application where I have a main form that remains open as long as the application is running.  Various other forms are opened from this main form and interact in various ways with an Access database.  Is it a good idea (performance, style, etc.) to open a connection to the database on this form and pass a reference to it to each of the forms the user may open   Then close it when the app exits.  Is this a bad design   What are the pros and cons of the approach

Thanks


Answer this question

Keeping connection open: Good Idea?

  • JOSE3007

    While there is nothing stopping you from doing it, keeping a db connection open for any longer than necessary is usually considered bad practice.  If the file is on a different machine, I'd definitely recommend opening the connection only when you need to use it, as any loss of contact with the file (network cable being unplugged, server crashing, etc.) would kill your app.

    If you're just looking to keep things simple by not having your ancillary forms have to manage connections, you could keep a single connection object in your main form and open and close on request from the other forms.

    If it's a small app with only one user at a time and the db is on the same machine as your app, the best practices police probably won't come looking for you if you keep the connection alive for the duration of the app :)

  • JGIS

    I don't know about the design issue, but it surely is a good idea performancewise. Opening the connection is an "expensive" operation.
    If your application uses multi-threading, make sure you only execute one command on the connection at any time. Whidbey will introduce MARS (Multiple Active Results Sets), that enables executing several commands over the same connection at the same time. (Not sure if that only works on SQL Server...)

  • Keeping connection open: Good Idea?