DB Connection Class!

A

Adam Knight

Hi all,

I am wanting to create a re-useable class to manage db connections in an
app.

This is what i have so far:


using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public class DBUtil
{

public DBUtil()
{

}

public static SqlConnection Conn
{

get {

//return DB connection
return Conn;

}
set {

//determine if a db connection is already established
if (Conn == null)
{

//establish a connection to the db
Conn = GetDBConnection();

}

}

}

public static SqlConnection GetDBConnection()
{

//retrieve connection string for db from web config
string ConnString =
ConfigurationManager.ConnectionStrings["TestCenterDB"].ConnectionString;

//establish a connection to the database
Conn = new SqlConnection(ConnString);

//open db connection
Conn.Open();

//return db connection
return Conn;

}

public static void CloseDBConnection(SqlConnection Conn)
{

//close db connection
Conn.Close();

}

}

Would appreciate any ones thought and suggestions on possibly a better
approach or improving what i have some far!!!!
..Is my logic ok? I suspect it isn't quite right???

Cbeers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top