Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Data Programming  »  Business Mate: Using the Generic Database Class
 »  Home  »  Data Programming  »  ADO.NET  »  Business Mate: Using the Generic Database Class
 »  Home  »  Data Programming  »  Microsoft Access  »  Business Mate: Using the Generic Database Class
Business Mate: Using the Generic Database Class
by Charles Profitt | Published  02/10/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...
Creating a dataSet and a DataView
We have to use the class we created. At the top of your code, above the namespace section, you have to add the following using statements:

using System.Data;
using System.Data.OleDb;

If you want to not have to write out the complete namespace of your class you will need to include that:

using System.Data;
using System.Data.OleDb;
using Business_Mate.classes;

The remainder of the example code will assume you used the first example and didn't type "using Business_Mate.classes;" I normally add an additional static void for each DataSet I want to retrieve more than once. The first thing I want to retrieve is a list of users with their associated addresses.

public static DataSet dsContactFullName()
{

string select =
     "SELECT contactID, fullName, street, city, state, zipcode FROM contactFullName";
DataSet dsContact = classes.database.ds(select, "contactFullName");
return dsContact;

}

This makes use of the generic DataSet code in the previous part of this series by passing a select string and table name. Now that I showed you that I make another function to return a specific DataSet I also prefer to "close" the generic function and make it private as detailed below:

private 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;

}

The difference is the keyword "private" instead of "public". This makes the function unavailable outside the class. Now if I wanted to to sort the data or filter it I would create a DataView.

public static DataView dvContactFullName(string filter)
{

DataSet ds = new DataSet();
ds = dsContactFullName();
DataView dv = new DataView(ds.Tables["contactFullName"]);
dv.RowFilter = filter;
return dv;

}

So you should now know how to take the generic dataset class and make a specific dataset and also a dataview. In the next part of this series I will cover using a dataset to fill a listbox.

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:3.7 out of 5
 10 people have rated this page
Article Score13180
Article Series
This article is part 2 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/09/2005)
Rating
Nice addition to the first segement.
 
Comment #2  (Posted by an unknown user on 03/13/2005)
Rating
Well worded and to the point. Good job
 
Comment #3  (Posted by an unknown user on 05/31/2006)
Rating
Great. Do complete the article.
 
Comment #4  (Posted by an unknown user on 11/17/2006)
Rating
Nice article - one observation : it would be better practice to put the objConnection.Close(); statement a 'finally {}' block to ensure that the connection is close regardless of any errors that may occur.
 
Comment #5  (Posted by an unknown user on 11/17/2006)
Rating
Nice article - one observation : it would be better practice to put the objConnection.Close(); statement a 'finally {}' block to ensure that the connection is close regardless of any errors that may occur.
 
Comment #6  (Posted by an unknown user on 09/13/2008)
Rating
though it is against the long-term ,
 
Comment #7  (Posted by an unknown user on 09/13/2008)
Rating
available to you is still the same.,
 
Comment #8  (Posted by an unknown user on 09/13/2008)
Rating
It works because reputable writers make links to things ,
 
Comment #9  (Posted by an unknown user on 09/13/2008)
Rating
idea of using the secretariat of the ,
 
Comment #10  (Posted by an unknown user on 09/13/2008)
Rating
Thank you.,
 
Sponsored Links