Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Data Programming  »  Business Mate: Generic Database Class
 »  Home  »  Data Programming  »  ADO.NET  »  Business Mate: Generic Database Class
 »  Home  »  Data Programming  »  Microsoft Access  »  Business Mate: Generic Database Class
Business Mate: Generic Database Class
by Charles Profitt | Published  02/08/2005 | Data Programming ADO.NET Microsoft Access | Rating:
Charles Profitt
Charles Profitt currently works as both a developer and system administrator for a K-12 school district. His diverse experience includes working with Netware, Active Directory, SQL Server (2000 and 2005), IIS 6, Lotus Notes and Visual Studio.Net (2002, 2003, and 2005). His language of choice is C#. Charles has created several windows and web bases applications in since November of 2002. 

View all articles by Charles Profitt...
Business Mate: Generic Database Class

I am no longer a beginner like I was back in  2002 when I wrote A Beginner's Guide to VB.NET and Database Programming - The Recipe Application for DevCity.NET. I wrote back then that "I am a beginner. I wanted to share some experiences with other beginners" and now I want to share with you what I have learned since then. I have made several applications and actually got paid to make two of them. In the process of developing, I graduated from the simple books I started with to more advanced material and even started to experiment on my own with the concepts I learned.

Eventually my goal will be to create a small database application that is a working functional piece of software. It will be more complex than The Recipe Application was and will make use of a local Access database. I will walk you through the concepts first and then in the end I will enclose a copy of the source code. Yes, this will be a long process that will cover everything from database classes to GUI creation and concepts.

The first item I want to cover is the generic database class. Beginners books show you how to make a DataConnection, DataAdapter and DataSet using the IDE; while that works I find it a poor way to do things. I prefer to understand a little more and excercise a little more control.

The Connection:
The first thing to do in a database class is to make a connection to the database. Go ahead and right click on your project in the solution explorer and select add class. You can name it dbConnection.

public class database

{

public static OleDbConnection GetConnection()
{

// TODO: must get application install path
string connection = 
@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Documents and Settings\UserName\Data\thedb.mdb";
return new OleDbConnection(connection);

}

// see below for more

}

I hard coded the path above, but the best procedure would be to get the application install path or have an application settings file that stored the location of the database. In this case the application is intended to be profile specific for the data so the data is stored in each user's documents and settings folders. This function creates a connection to the database and can be called from any other part of your code. I prefer to extend my database class to include creating DataSets and DataViews and pass those to my pages (asp.net) or forms (windows). So let's take a look at that...

static DataSet ds(string select, string table)
{

OleDbConnection objConnection = GetConnection();
OleDbCommand objCommand =
     

new OleDbCommand(select, objConnection);
OleDbDataAdapter da = new
OleDbDataAdapter(objCommand);
DataSet ds = new
DataSet();
objConnection.Open();

try
{

da.Fill(ds, table);

}

catch (OleDbException e)
{

MessageBox.Show(e.ToString());

}
objConnection.Close();
return ds;

}

Here we have a function that returns a DataSet public static DataSet ds(string select, string table) that requires the SELECT string for selecting fields and the name of the table. This allows you to reduce the amount of code you will need to write to get a DataSet to any form or page in the application you are creating or to build a DataSet so that you can create a DataView.

Well, that is it for this short example of how to make a generic Database Class. In the next part I will show you how to use the class above to make more specific classes for building menus, drop down boxes, or even filling datagrids. I wll show you how to create a DataView and give you my thoughts on when to use them.

Until then keep coding.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:2.8076923076923 out of 5
 52 people have rated this page
Article Score20740
Sponsored Links