473,382 Members | 1,563 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,382 software developers and data experts.

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

Similar topics

4
by: Linus Nikander | last post by:
Having recently load-tested the application we are developing I noticed that one of the most expensive (time-wise) calls was my fetch of a db-connection from the defined db-pool. At present I fetch...
5
by: alejandro lapeyre | last post by:
How can I load / parse an HTML file with .NET? Thanks! Best regards, Alejandro Lapeyre
4
by: Krista Lemieux | last post by:
Hello, I know this is probably a hudge topic to discuss and there are lots of different ways of implementation, but I still would like to ask and hear the most commonly used techniques for this....
2
by: Bryan | last post by:
Hello, I'm just starting to develop in asp.net and i have a question about using a database connection globally in my app. I have set up the procedures for getting all my connection string info...
3
by: Martin B | last post by:
Hallo! I'm working with C# .NET 2.0, implementing Client/Server Applications which are connecting via Network to SQL-Server or Oracle Databases. To stay independent from the underlaying Database...
35
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection...
8
by: Imran Aziz | last post by:
Hello All, Like in C++ I tried to use constructor to open a database connection and distructor to close the database connection, it now turns out that one cannot create distrutors in C# classes. ...
7
by: =?Utf-8?B?QVRT?= | last post by:
HOWTO Run multiple SQL statements from ASP/ADO to an Oracle 10g. Please help, I'm trying to write an ASP page to use ADO to run a long query against an Oracle 10g database, to create tables,...
20
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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...

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.