Windows Search (WDS 4)
Written by Mike James   
Monday, 22 February 2010
Article Index
Windows Search (WDS 4)
Getting Started
Making the connection
Binding to a grid
Binding in WPF
More properties
Scope and content

 

Getting started

Start a new Windows Application C# project, it can be either a Windows Forms or a WPF project, called iSearch using C# Express or a full copy of Visual Studio. Notice that everything works exactly the same under Windows Forms or WPF apart from the databinding and specifically the DataGrid class. If you want to add a WPF DataGrid then the best route is to use .NET 4.0 via Visual Studio 2010 or Express 2010.

Add to the start of the code:

 using System.Data.OleDb;

and place a button on the form to run the search code when it is clicked.

There are two distinct ways to work with a database via ADO .NET – typed or untyped.

Typed or untyped?

If you opt for the untyped approach then all of the data returned by a query is of type object and you have to cast it to whatever type it really is every time you use it. If you only use it once then this isn't too bad but if your program uses it repeatedly it's a nuisance.

There is also the point of view that data should be "strongly" typed simply so that the compiler can detect errors when you attempt to assign a string to a number say.

The central object of ADO .NET is the DataSet which you can consider to be a collection of Tables. Each Table object roughly corresponds to a database table with rows and columns.

In a complex situation the tables in a DataSet can be linked to form a relational database but stored in memory rather than on disk. In this case things are simpler and we need a DataSet that contains a single table which is the result of a query on the search index. We also only need to define the columns that the query actually returns.

So our first job is to create a suitable DataSet to hold the data that results from the query.

As already mentioned there are two ways to do this – strongly typed or untyped. The strongly typed approach uses a Wizard to generate a special class that inherits from DataSet with additional typed properties – one for each table and each column we define. This is a fairly easy way to work so let's use the Wizard to create a typed DataSet.

All of the following will work under Visual Studio/Express 2008 or 2010 without modification.

Use the command Project,Add New Item and select DataSet in the dialog box that appears. Call the new DataSet

 SearchResults.xsd 

which is the name of an XML file used to store the database definition or "schema" which is used to generate the new class.

dataset
Add a new "typed" DataSet to the project

After you have added the new DataSet you will see the DataSet designer which you can use to add tables and then columns to the tables.

In this case we only need a single table called "Table". It has to be called "Table" because this is the name returned by the index query.

You can add a new table either by right-clicking or by dragging a table from the toolbar. Once you have changed the table's name you can add a column called System.FileName, which by default is specified to be a string. Later you can add additional columns to the table but for the moment we are only going to return the FileName property from the query and so this is the only one we actually need.

design
Using the designer to add a table and a single column

<ASIN:0470279338>

<ASIN:0470584661>

<ASIN:1590599535>
<ASIN:059652028X >

Last Updated ( Sunday, 21 February 2010 )