473,662 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,par am,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,par am)

or any better practice?
Nov 17 '05 #1
4 4881
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*********@an gelfire.com> wrote in message
news:a0******** *************** *********@4ax.c om...
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,par am,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,par am)

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@_spamkille r_bobpowell.net > wrote in message
news:OM******** ********@TK2MSF TNGP14.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*********@an gelfire.com> wrote in message
news:a0******** *************** *********@4ax.c om...
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,par am,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,par am)

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 "(DynamicProper ties)" 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*********@an gelfire.com> wrote in message
news:l3******** *************** *********@4ax.c om...

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
6115
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 my connections using : private Connection getConnection() throws SQLException { try { Context jndiCntx = new InitialContext(); DataSource ds = (DataSource)
5
7112
by: alejandro lapeyre | last post by:
How can I load / parse an HTML file with .NET? Thanks! Best regards, Alejandro Lapeyre
4
2205
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. Basically I have an ASP.NET application, and my connection string currently is stored in a constant public variable in one of my classes. The reason for that is so that I only have one place to change the connection string when I deploy it on a...
2
3488
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 which each page will use, but my question relates to how to use the database connection i create in all my classes. I have a database class, in a separate namespace and file, i created that handles all the connection opening, executing statements...
3
10287
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 I use System.Data.Common.DBConnection and .DBCommand. How can I keep aware from connection losses (network not availeable, db-server not available...)? Are there any strategies to detect this broken connections, and how can I
35
4831
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 string in the web.config. I created a class with a static database connection and the class opens and closes the database.
8
14554
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. Here is my code public class DBLayer {
7
20318
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, if they do not already exist. In terms of ASP/ADO, that would be fine in a SQL Server Sense by a simply ASP/Server-Side JavaScript as such: var cnTemp = Server.CreateObject("ADODB.Connection");
20
3271
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 many connection has been used ? 2. If the maximum pool size has been reached, what happens when I call the method Open to open the connection ? Will I get an error ? MSDN says the request is queued, but will I get an error in the open method ? ...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8764
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.