473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 3010

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Job Lot | last post by:
I have written the following function which returns OleDbConnection object. If global variable g_strDbPath contains an invalid file name or path name the catch block throws an error with...
1
by: Lilly | last post by:
Hi all. I'm really sorry for this post, I'm sure most of you will think it's a silly question, but the following doesn't work and I'm really new to ASP.NET: <%@ Page Language="C#" %> <%@...
1
by: Matthias Kwiedor | last post by:
Hello! My Application is Multi-Threaded, where i have several threads which get datas and write them to the database, or have to read from the database to compare the datas and make updates if...
0
by: Seth | last post by:
First off, my apologies if this is in the wrong newsgroup, but I hope I'm close enough. I'm trying to do some parsing of a CSV file using OleDbConnection, but for some reason, when I populate my...
0
by: Grant | last post by:
My C# web application connects to an Access database using the OleDbConnection and OleDbDataReader. I have 3 other computers that connect to this server but very frequently I get an 'Unspecified...
2
by: Grant | last post by:
My C# web application connects to an Access database using the OleDbConnection and OleDbDataReader. I have 3 other computers that connect to this server but very frequently I get an 'Unspecified...
0
by: Tim | last post by:
I work on a dll with mfc support in C++ .NET. I include the following statemwents, but the complier does not allow me to do OleDbConnection * cnNwind = new OleDbConnection() The error msg says...
1
by: Keith | last post by:
Hi, if have this method in a cs file that connections to a database and returns a datareader to my web form. public OleDbDataReader GetDataReader(string theSQL) { theConn = new...
3
by: Ludvig | last post by:
This Exception is executed when using SQLConnection. OleDbConnection connects fine. System.EnterpriseServices.Platform.Initialize() +503...
1
by: Souloron | last post by:
Here what I did: string cmdText = String.Format(("CREATE TABLE {0} (dno int primary key, Sunday char(5), Monday char(5), Tuesday char(5), Wednesday char(5), Thursday char(5),...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.