Connecting Tech Pros Worldwide Forums | Help | Site Map

howto make a connection to database available in my classes.

Digital Fart
Guest
 
Posts: n/a
#1: Nov 17 '05
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?



Bob Powell [MVP]
Guest
 
Posts: n/a
#2: Nov 17 '05

re: howto make a connection to database available in my classes.


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" <progressdll@angelfire.com> wrote in message
news:a0fgn1dblopectbu8l3cl98iuad0t0jvqr@4ax.com...[color=blue]
> 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?
>
>[/color]


John Richardson
Guest
 
Posts: n/a
#3: Nov 17 '05

re: howto make a connection to database available in my classes.


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:OMT06%23O6FHA.1248@TK2MSFTNGP14.phx.gbl...[color=blue]
> 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" <progressdll@angelfire.com> wrote in message
> news:a0fgn1dblopectbu8l3cl98iuad0t0jvqr@4ax.com...[color=green]
>> 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?
>>
>>[/color]
>
>[/color]


Digital Fart
Guest
 
Posts: n/a
#4: Nov 17 '05

re: howto make a connection to database available in my classes.



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.
[color=blue]
>another good spot is the App.Config file which allows for different conn
>strings for different users... Enterprise Library uses this, I think.
>
>[/color]

John Richardson
Guest
 
Posts: n/a
#5: Nov 17 '05

re: howto make a connection to database available in my classes.


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" <progressdll@angelfire.com> wrote in message
news:l3pgn1d10dbs9o9l40mql6pmli46fc7fqu@4ax.com...[color=blue]
>
> 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.
>[color=green]
>>another good spot is the App.Config file which allows for different conn
>>strings for different users... Enterprise Library uses this, I think.
>>
>>[/color]
>[/color]


Closed Thread