Solved

SQL Database Conenction Class

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 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