Introduction
DataSets are powerful data structures with the capability of allowing the programmer to treat them as in-memory databases. Recently I had a project that required me to do queries from an Access Database to retrieve data. If you do enough queries in MS Access, the time to access a table becomes a noticeable performance issue. (I suspect there is a lot of file I/O going on each time you do a query.). In order to reduce the time in querying the database, I thought to myself, "Why not just read the whole database into a DataSet?". Then you can just do all your queries in memory through the DataTable Select call.
Importing the Database into a DataSet
The first step is to get the data into the DataSet. The way we do this is to do a few wildcard queries on the tables we care about in the DataSet through the OleDbAdapter. We will need to create an adapter for each table that we want to import from MSAccess. Then we can just call Fill on the DataSet using each of the OleDbAdapters.
Listing 1 - Filling the DataSet from the Access Database
// connect to the Access Database
OleDbConnection ConnectToDatabase()
{
string managedConnString = GetDefinitionDatabaseConnectionString();
_conn = new OleDbConnection(managedConnString);
_conn.Open();
return _conn;
}
// Read in all the data in the tables we are interested in
void SetupDatabaseInMemory
{
// connect to the Access Database
_conn = ConnectToDatabase();
// create an oledb adapter containing a wildcard query for each table we are interested in
OleDbDataAdapter customerAdapter= new OleDbDataAdapter("SELECT Customer.* from [Customer]", _conn);
OleDbDataAdapter orderAdapter = new OleDbDataAdapter("Select * from Order", _conn);
OleDbDataAdapter productAdapter = new OleDbDataAdapter("Select * from Product", _conn);
// fill a dataset with all of the tables we are interested in using the adapters
_ds = new DataSet();
customerAdapter.Fill(_ds, "Customer");
orderAdapter.Fill(_ds, "Order");
productAdapter.Fill(_ds, "Product");
}
Now that we have data from all the three tables that we are interested in memory, we can do all are querying in memory using the Select method of the DataTable. We can wrap this method with the ExecuteQuery method shown below. This method specifies both the table name we are querying and the filter string (WHERE clause) that we are using to filter the data. Once we do the query, we want to take the resulting rows and put them in a result DataSet. We do this by cloning the DataTable structure and then importing the DataRows in the query result into the table.

Aaron VanWierenPosted Jan 10, 2007, 2:39 PM
Good article, basic but good. I have not seen alot posted dealing with handling and manipulating datatables. I noticed that in your aritcle in Listing 2, you use the clone method then you clear: // clone the existing table structure and clear it out DataTable copyTable = _ds.Tables[tableName].Clone(); copyTable.Rows.Clear(); You actually do not need the second row as the clone() method only copies database schema information. If you comment it out, it should still work the exact same as there are no rows in a cloned copy. Just an observation, good article. It would be great to see more similar articles (maybe I'll contribute, as I have been working on an article on using the compute method). Aaron W. VanWieren