473,372 Members | 1,549 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,372 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 3008

"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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.