471,570 Members | 1,632 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

How do I use only one OleDbConnection

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

Dec 12 '05 #1
6 2967

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:lx*********************@newsc.telia.net...
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

Dec 12 '05 #2
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.

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:lx*********************@newsc.telia.net...
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

Dec 12 '05 #3
Dear Tony,
You can create a Singleton Class for database
Connection.

Regards,
Naveed Ahmad Bajwa
http://bajoo.blogspot.com/

Dec 12 '05 #4
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:lx*********************@newsc.telia.net...
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

Dec 12 '05 #5
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Tony Johansson" wrote:
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

Dec 12 '05 #6
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.

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:DC**********************************@microsof t.com...
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Tony Johansson" wrote:
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

Dec 12 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Job Lot | last post: by
1 post views Thread by Matthias Kwiedor | last post: by
reply views Thread by Grant | last post: by
2 posts views Thread by Grant | last post: by
3 posts views Thread by Ludvig | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.