This article has been excerpted from the book "A Programmer's Guide to ADO.NET in C#".
Now you'll create your first sample using data adapters. In this sample example, I'll show you how to create data adapters using Sql and OleDb data providers and fill data from the data adapter to a DataGrid control.
First, create a Windows application using Visual C# projects and add two buttons and a DataGrid control to the form by dragging the controls from the toolbox to the form. Second, set both button's Name property; use OleDbDataAdapter and SqlDataAdapter. Next set the properties to OleDbData Adapter and SQL DataAdapter. After setting these properties, the form will look like figure 5-40. As you can see, there are two buttons, OleDbDataAdapter and SQL DataAdapter.
Now add button-click event handlers for both the OleDbDataAdapter and SQL DataAdapter buttons. You can add a button-click event handler either by double-clicking on the button or by using the Events tab of the Properties window of the button control. On the OleDb DataAdapter button-click event handler, you'll write code to read data from an OleDb data source and fill data to the data grid. On the SQL DataAdapter button-click event handler, you'll write code to read data from a SQL Server datasource and fill data to the data grid.
Figure 5-40. Creating a Windows Forms application and adding controls to the form
Listing 5-44 shows the source code for the OleDb DataAdapter button click, and Listing 5-44 shows the source code for the SQL DataAdapter button click, and Listing 5-45 shows the source code for the SQL DataAdatper button click. As you can see, you follow the same steps as before. Open a connection, create a data adapter object with a SELECT string, create a dataset object, call the data adapter's FILL method to fill the dataset, and bind the dataset to the DataGrid.DataSource property as DataSet.DefaultViewManager, which represents the default view of a DataSet object.
Listing 5-44. Displaying the Order table data in a Data Grid