Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Database Conenction Class

Posted on 2006-11-07
7
Medium Priority
?
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

604 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