468,242 Members | 1,685 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,242 developers. It's quick & easy.

programming style question (mysql connection)

Hi.

I have to use a database in my c# program.
For that I created a MySqlManager class.
If it is being instanciated a connection will be established like this:

odbcConnection = new OdbcConnection(connectionTarget);
odbcConnection.Open();
(where connectionTarget is a string describing the database access)

I do not close this connection until the main program is terminated.
Is that a good way or is it better to open and close it for every task?

I am asking because Ive got a problem with the database access.
My program is running 5 or 6 times great and after that I receive an
odbc connection error. I thought that it could be possible that I get
this error if I do not close the connection properly?!?!

After logging in to windows again everything is working fine - for 5 or 6 times...

Thanks for any tips and tricks,
timtos.
Nov 15 '05 #1
3 1502
It sounds like the connection is getting closed correctly...

But as far as the other question, there is a trade-off and both have pros
and cons. When you open/close the database every time - that saves resources
on your database but might appear slower on your front-end. With leaving the
connection open, you need to put extra error handling all over the place in
case the server goes down and you are eating up a connection with every
instance of your application.

Since it's mySQL and in this particular case, I'd say do the open/close
method...

"timtos" <ti****@gmx.de> wrote in message
news:bi**********@news.uni-koblenz.de...
Hi.

I have to use a database in my c# program.
For that I created a MySqlManager class.
If it is being instanciated a connection will be established like this:

odbcConnection = new OdbcConnection(connectionTarget);
odbcConnection.Open();
(where connectionTarget is a string describing the database access)

I do not close this connection until the main program is terminated.
Is that a good way or is it better to open and close it for every task?

I am asking because Ive got a problem with the database access.
My program is running 5 or 6 times great and after that I receive an
odbc connection error. I thought that it could be possible that I get
this error if I do not close the connection properly?!?!

After logging in to windows again everything is working fine - for 5 or 6 times...
Thanks for any tips and tricks,
timtos.

Nov 15 '05 #2
Thanks for your help!
It helped me!

Greetings,
timtos.

"Frank Drebin" <no*****@imsickofspam.com> wrote in message news:qt**********************@newssvr28.news.prodi gy.com...
It sounds like the connection is getting closed correctly...

But as far as the other question, there is a trade-off and both have pros
and cons. When you open/close the database every time - that saves resources
on your database but might appear slower on your front-end. With leaving the
connection open, you need to put extra error handling all over the place in
case the server goes down and you are eating up a connection with every
instance of your application.

Since it's mySQL and in this particular case, I'd say do the open/close
method...

"timtos" <ti****@gmx.de> wrote in message
news:bi**********@news.uni-koblenz.de...
Hi.

I have to use a database in my c# program.
For that I created a MySqlManager class.
If it is being instanciated a connection will be established like this:

odbcConnection = new OdbcConnection(connectionTarget);
odbcConnection.Open();
(where connectionTarget is a string describing the database access)

I do not close this connection until the main program is terminated.
Is that a good way or is it better to open and close it for every task?

I am asking because Ive got a problem with the database access.
My program is running 5 or 6 times great and after that I receive an
odbc connection error. I thought that it could be possible that I get
this error if I do not close the connection properly?!?!

After logging in to windows again everything is working fine - for 5 or 6

times...

Thanks for any tips and tricks,
timtos.


Nov 15 '05 #3
Timtos:

The answer depends on what you are doing with it. If you
are using a DataReader or any of the commandfunctions
like ExecuteScalar or NonQuery..

The other consideration is how many calls to the db are
being done.

1) If you are using an Adapter to fill datatables
(disconnected ADO.NET), then the Adapter will take care
of opening and closing the connections...don't do it
yourself.

2) If you are using the connection to submit updates
only, or fire execute scalar commands periodically, then
close it. The main metric to consider is the frequency
with which you'll make trips to the db. Leaving
connections open is the antithetical to the whole ADO.NET
thinking ---- after all, something that works with one
user might turn into a nightmare with 1000 users. Which
begs the next question, how many users do you have? THe
more users you have, the more you'll want to close those
connections.

I hope this helps,

Bill

W.G Ryan
do********@comcast.nospam.net
www.knowdotnet.com
-----Original Message-----
Hi.

I have to use a database in my c# program.
For that I created a MySqlManager class.
If it is being instanciated a connection will be established like this:
odbcConnection = new OdbcConnection(connectionTarget);
odbcConnection.Open();
(where connectionTarget is a string describing the database access)
I do not close this connection until the main program is terminated.Is that a good way or is it better to open and close it for every task?
I am asking because Ive got a problem with the database access.My program is running 5 or 6 times great and after that I receive anodbc connection error. I thought that it could be possible that I getthis error if I do not close the connection properly?!?!

After logging in to windows again everything is working fine - for 5 or 6 times...
Thanks for any tips and tricks,
timtos.
.

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Mike Chirico | last post: by
4 posts views Thread by Reinier Beeckman | last post: by
9 posts views Thread by eholz1 | last post: by
oll3i
5 posts views Thread by oll3i | last post: by
3 posts views Thread by tulika dutta | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.