470,815 Members | 1,277 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

howto make a connection to database available in my classes.

howto make a connection to database available in my classes.

What is the best practice when i want to write classes
that need a connection to the database?

Do i make a conn variable in my main() and give
it as a parameter to every object i make that needs
access to the database

ex.

A class called Price that need to do
some bussines logic to a database.

Price price = new price(param,param,connection)

or do i connect to the database in every class
i build

ex.

A class called Price that does a connection itself.

Price price = new price(param,param)

or any better practice?
Nov 17 '05 #1
4 4754
Re-create the connection as-needed from a constant connection string and
allow connection pooling that's built in to the system to manage this for
you.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Digital Fart" <pr*********@angelfire.com> wrote in message
news:a0********************************@4ax.com...
howto make a connection to database available in my classes.

What is the best practice when i want to write classes
that need a connection to the database?

Do i make a conn variable in my main() and give
it as a parameter to every object i make that needs
access to the database

ex.

A class called Price that need to do
some bussines logic to a database.

Price price = new price(param,param,connection)

or do i connect to the database in every class
i build

ex.

A class called Price that does a connection itself.

Price price = new price(param,param)

or any better practice?

Nov 17 '05 #2
another good spot is the App.Config file which allows for different conn
strings for different users... Enterprise Library uses this, I think.
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:OM****************@TK2MSFTNGP14.phx.gbl...
Re-create the connection as-needed from a constant connection string and
allow connection pooling that's built in to the system to manage this for
you.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Digital Fart" <pr*********@angelfire.com> wrote in message
news:a0********************************@4ax.com...
howto make a connection to database available in my classes.

What is the best practice when i want to write classes
that need a connection to the database?

Do i make a conn variable in my main() and give
it as a parameter to every object i make that needs
access to the database

ex.

A class called Price that need to do
some bussines logic to a database.

Price price = new price(param,param,connection)

or do i connect to the database in every class
i build

ex.

A class called Price that does a connection itself.

Price price = new price(param,param)

or any better practice?


Nov 17 '05 #3

So when i look at this example
http://www.csharphelp.com/archives4/archive617.html

The use the App.Config for a connection string.

So c# best practice is to simply connect every time you need some data
and close in the same routine that was reading the data.
another good spot is the App.Config file which allows for different conn
strings for different users... Enterprise Library uses this, I think.


Nov 17 '05 #4
I think that's a best practice across most languages now (opening and
closing the conn).
Stick it in a using block though, to guarantee the conn closes... that way
if you error out in your code block, the conn is guaranteed to close. I
think that code is a poor example because they omitted that. If (when) you
use the VS designer, you can stick the conn string in the App.config file
using the "(DynamicProperties)" section in the properties window, if you
have added the SqlConnection object to your form through the designer
(through the server explorer). VS handles it for you. Or you can add it
manually, of course.

"Digital Fart" <pr*********@angelfire.com> wrote in message
news:l3********************************@4ax.com...

So when i look at this example
http://www.csharphelp.com/archives4/archive617.html

The use the App.Config for a connection string.

So c# best practice is to simply connect every time you need some data
and close in the same routine that was reading the data.
another good spot is the App.Config file which allows for different conn
strings for different users... Enterprise Library uses this, I think.

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by alejandro lapeyre | last post: by
4 posts views Thread by Krista Lemieux | last post: by
2 posts views Thread by Bryan | last post: by
3 posts views Thread by Martin B | last post: by
35 posts views Thread by Terry Jolly | last post: by
8 posts views Thread by Imran Aziz | last post: by
20 posts views Thread by fniles | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.