How do I use only one OleDbConnection

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

We have a database and several forms that access the database. When we
access the database from all the different forms we only want to have one
OleDbConnection that all the forms use. I assume that must be possible
because creating a OleDbConnection for each form takes a lot of time.

Where would I put this OleDbConnection so all the other forms understand
that a connection exist?

//Tony
 
Tony Johansson said:
Hello!

We have a database and several forms that access the database. When we
access the database from all the different forms we only want to have one
OleDbConnection that all the forms use. I assume that must be possible
because creating a OleDbConnection for each form takes a lot of time.

Where would I put this OleDbConnection so all the other forms understand
that a connection exist?

//Tony
 
Well, you can always create a utility class that will keep an instance in a
static variable and each for could then ask for that instance, like a
singleton.
 
Hi,

You can keep the instance as a static member of some class.
Or even better, you could use a similar approach but do not make public the
connection, instead make public the methods (ExecuteScalar,
ExecuteReader,etc ) that access the data, these method will all share the
connection internally. This will allow you to make sure that the connection
is always closed as soon as you finish with it. ( Except in the case of
ExecuteReader where the connection is open while the reader is open)

cheers,
 
Actually if you use the same connection string, the connection will be
recycled from the connection pool, which is probably the most efficient way
to handle this.

If you use a helper class such as the Application Blocks SqlHelper class, it
will even cache the SqlParameters for you, and you can make a stored
procedure call in just 2 lines of code.
Peter
 
Peter is the only one that has given the correct answer. Take his advice.
Connection Pooling is built into the .Net framework for this very reason. If
you work with it, you will do well. If you fight it, you will not.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top