Solved

SQL Database Conenction Class

Posted on 2006-11-07
7
185 Views
Last Modified: 2010-04-16
Hello All
I am trying to learn more about OOP. I am trying to create a Database Connection Class. I have created other classes and methods, but this one is giving me great trouble for some reason.

I have code in the .cs file that currently works fine to open and close the connection. But that conenction method is written on each and every page. As we all know, that is not a good thing.

A sample of code or a place to find the answer would be greatly appreciated.

Thanks
Andrew SQLDBA
0
Comment
Question by:AndrewSQLDBA
  • 2
7 Comments
 
LVL 12

Expert Comment

by:RubenvdLinden
ID: 17890295
Can you paste some of your code? I don't really understand what's your problem.
0
 

Author Comment

by:AndrewSQLDBA
ID: 17893922
If I had code that was in a class, I would not have asked.

I want to create a class, that the code does a connection to a SQL database. How do I code that using C#

Andrew SQLDBA
0
 
LVL 8

Accepted Solution

by:
sabeesh earned 50 total points
ID: 17895490
hi
     You can write a class like this and you

public class Connection
{
private static Connection connection = null;
protected Connection()
{}
public void Open()
            {
                  string connString = string.Empty;

                  // Close the connection if it is already open
                  if ((dbConnection != null) && 
                        (dbConnection.State == ConnectionState.Open))
                  {
                        Close();
                  }

                  dbConnection = new SqlConnection(connectionString);
                  dbConnection.Open();
            }
                ///  Static method that creates(if not exist)
            ///  and returns the singleton Connection object.
            ///  Users of this class will access this method to get the
            ///  object instance outside.
            ///  This will ensure creation of the object happens only in
            ///  this static method.
public static Connection GetObject()
            {
                  if (connection == null)
                        connection = new Connection();
                  return connection;
            }
}


and you can open the connection from your other pages like this

Connection.GetObject().Open();
0
 
LVL 12

Expert Comment

by:RubenvdLinden
ID: 17900363
It's good practice to create a new connection object on each page and dispose it when you're finished using it.
Sure, it's a lot of code but using a static connection is not thread-safe.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now