?
Solved

SQL Database Conenction Class

Posted on 2006-11-07
7
Medium Priority
?
216 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month8 days, 19 hours left to enroll

764 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