Solved

SQL Database Conenction Class

Posted on 2006-11-07
7
182 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

19 Experts available now in Live!

Get 1:1 Help Now