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:5 out of 5
 5 people have rated this page
Article Score12847
Article Series
This article is part 1 of a 2 part series. Other articles in this series are shown below:
  1. Business Mate: Generic Database Class
  2. Business Mate: Using the Generic Database Class
Related Articles
Comments    Submit Comment

Comment #1  (Posted by an unknown user on 02/08/2005)
Rating
This is a good start I look forward to the next lesson.
 
Comment #2  (Posted by an unknown user on 02/21/2005)
Rating
Dear Unknown Author:

Your statement, "the class being constructed is not generic in any way" is simply not correct. Certainly it could be more generic, or more uber-elite by including every type of data connection know to man but that doesn't make it non-generic.

The generic part of the class is that I can create a dataset by passing the table name and selection string to the class - that is generic; period.

I find the article to be a good general example that shows a programmer the way to make a database class. It is up to the person learning to adapt it to their needs.
 
Comment #3  (Posted by an unknown user on 05/31/2006)
Rating
Practical & proffessional
 
Comment #4  (Posted by an unknown user on 07/17/2006)
Rating
Well thought-out and designed post. Includes some good code samples, and answers the questions it claimed to. Also promises more in the series.
 
Comment #5  (Posted by an unknown user on 02/03/2007)
Rating
Very good. I have already implemented a similar class before reading this using vb.net. A suggestion to make the class more configurable would be to do as I have done and obtain the connection string from the web.config / app.config. Thus when you want to change it you do not need to recompile.

Regards

Scott
 
Sponsored Links