O'Reilly logo

ADO.NET Cookbook by Bill Hamilton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

7.17. Using ADO.NET Design-Time Features in Classes Without a GUI

Problem

The design-time environment provides controls and wizards to facilitate creation of and management of properties of ADO.NET objects. You want to use that design-time functionality when creating classes that do not have a GUI.

Solution

Create a component and use its design-time functionality.

The solution contains two parts: the component and the test container for the component. To create the component Component0717.cs, add two controls to its design surface:

  • Drop a SqlDataAdapter onto the design surface and use the Data Adapter Configuration Wizard to connect to the Northwind sample database on the local SQL Server. Accept all wizard defaults; supply the following SQL statement when prompted to generate the SQL statements:

    SELECT
        OrderID,
        CustomerID,
        EmployeeID,
        OrderDate,
        RequiredDate,
        ShippedDate,
        ShipVia,
        Freight,
        ShipName,
        ShipAddress,
        ShipCity,
        ShipRegion,
        ShipPostalCode,
        ShipCountry
    FROM
        Orders

    After the wizard completes, rename the SqlDataAdapter control to da.

  • A SqlConnection control is automatically added to the design surface when the SqlDataAdapter wizard is completed; rename it to conn.

The sample code for the component exposes one property and one method:

MyDataTable

A read-only property that returns a DataTable filled using the SqlDataAdapter control.

Update( )

This method takes a DataTable object argument that uses the SqlDataAdapter control to update changes to the DataTable (retrieved using the MyDataTable ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required