Creating a Database Viewer with Visual Studio and ADO.NET

I l @ ve RuBoard

Follow these steps carefully to create the application.

  1. Create a new Windows Forms project, and call it DBBind .

  2. Drag a DataGrid from the toolbox and position it on the form. Select the DataGrid properties and set the Dock property to Fill by clicking the center button in the UI editor that pops up for the Dock property.

  3. From the toolbar, select an OleDbDataAdapter from the Data tab. When you place it on the form, you'll see the Data Adapter Configuration Wizard dialog.

  4. On the first page of the wizard, select the Northwind database. You may need to click New Connection, select Microsoft Jet 4.0 OLE DB Provider in the Provider tab, press Next, and then browse for northwind.mdb . When you find it, or another database with which you are familiar, press the Test Connection button to ensure that the database is useable. Press OK on the New Connection dialog and press Next on the wizard.

  5. Select the radio button marked Use SQL Statements. Press Next.

  6. Type in a query that you know or select the query builder to create querys from the tables in the connected database. For our example, we used the query builder, added the Customers table from NorthWind , selected the check box marked All Columns on the table shown, and pressed OK on the query builder dialog.

  7. Pressing Next shows you the successful creation of the adapter and the connection, and you can click the Finish button. At this point, you will have a form with two objects, an OleDbDataAdapter and an OleDbConnection in the icon tray.

  8. Select the OleDbDataAdapter in the icon tray. Below the property browser, you will see a link that reads Generate DataSet. Click this link to create a dataset. Simply click OK on the following dialog; the default values are good. Now you'll have three icons in the tray.

  9. Select the DataGrid on the form again and edit the DataSource property using the drop-down combo box provided. You will see several data sources. If you have followed along and used the NorthWind database, select the dataSet11.Customers choice. When you do this, you'll see the DataGrid on the form begin to display all the column headers for the database.

  10. The final step is a manual one. You need to edit the constructor of the class to add the Fill command. Add the following line of code after the InitializeComponent call in the class constructor;


    Now you can build and run the program by pressing F5.

You will see the application shown in Figure 3.3.4, which will allow you to browse, order, edit, and generally muck-about with the NorthWind Customers table.

Figure 3.3.4. Complex DataBinding of the DataGrid to a database.


As an education, go back into the Visual Studio editor again and find the InitializeComponent method. It will probably be folded away in a little region and you'll have to expand it. Take a look at the code added by the DataAdapter wizard to set up the OleDataAdapter connection and the four datbase command objects.

As performing the walk through will generate the code for you, we won't list it here and save a small forest or two. You can clearly see that the wizards and helpers in ADO.NET really save your fingers.

I l @ ve RuBoard

C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204 © 2008-2017.
If you may any questions please contact us: