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 ...

Get ADO.NET Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.