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.