PC Review


Reply
Thread Tools Rate Thread

DB Connection Class!

 
 
Adam Knight
Guest
Posts: n/a
 
      26th Apr 2007
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,


 
Reply With Quote
 
 
 
 
sloan
Guest
Posts: n/a
 
      26th Apr 2007

You might want to not reinvent the wheel.

http://davidhayden.com/blog/dave/arc...1/08/2686.aspx

http://www.google.com/search?hl=en&q...%22+%22data%22


I would rely on "open late" / "close early".

But the source code for the EnterpriseLibrary would provide ideas if you
want to keep going .





"Adam Knight" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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,
>
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ado.net connection class? Rico Microsoft ADO .NET 2 15th Nov 2006 06:50 PM
Using Connection Class VS Connection Module Dave Johnson Microsoft ASP .NET 5 11th Dec 2005 01:28 PM
Using Connection Class VS Connection Module in Asp.net Application Dave Johnson Microsoft ADO .NET 4 10th Dec 2005 12:13 PM
Connection class John Microsoft ADO .NET 1 16th Sep 2003 08:32 PM
Having a connection class that only does connection work Jim Microsoft C# .NET 2 13th Sep 2003 11:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:58 AM.