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

Table of contents:

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 property of the component) back to the database.

The C# code for the component is shown in Example 7-33.

Example 7-33. File: Component0717.cs

// Namespaces, variables, and constants
using System;
using System.Data;
using System.Data.SqlClient;

// . . . 

public DataTable MyDataTable
{
 get
 {
 // Fill a table using the DataAdapter control.
 DataTable dt = new DataTable( );
 da.Fill(dt);

 return dt;
 }
}

public void Update(DataTable dt)
{
 // Update the table back to the data source.
 da.Update(dt);
}

The test container sample code contains two event handlers:

Form.Load

Instantiates the component Component0717 and binds the default view of the DataTable that it exposes (through the MyDataTable property) to the DataGrid on the form.

Update Button.Click

Instantiates the component Component0717 and calls the Update( ) method of the component to update changes made in the DataGrid to the DataTable retrieved in the Form.Load event handler.

The C# code for the test container is shown in Example 7-34.

Example 7-34. File: UsingDesignTimeFeauresWithComponentsForm.cs

// Namespaces, variables, and constants
using System;
using System.Data;

// . . . 

private void UsingDesignTimeFeauresWithComponentsForm_Load(object sender,
 System.EventArgs e)
{
 // Bind the default of the table from the component to the grid.
 Component0717 c = new Component0717( );
 dataGrid.DataSource = c.MyDataTable.DefaultView;
}

private void updateButton_Click(object sender, System.EventArgs e)
{
 // Update the table to the data source using the component.
 Component0717 c = new Component0717( );
 c.Update(((DataView)dataGrid.DataSource).Table);
}

Discussion

The component and control are special-purpose classes in the .NET Framework:

  • A component is a class that implements the IComponent interface or inherits from a class that implements that interface, such as System.ComponentModel.Component . A component has no user interface; it supports a visual design surface similar to the component tray on controls with a visual design surface that you can use to add or arrange components used by the component. The arrangement of components on the design surface is not important because there is no user interface for a component.
  • A control is a component that provides user interface functionality and inherits from the System.Windows.Forms.Control class, which in turn derives from the Component . A control is a component, but a component is not necessarily a control.

To create a component, simply right-click on a project or folder in the Solution Explorer window and select Add Add Component. The View Designer button in the Solution Explorer window accesses the design surface of the component.

Working with XML

Connecting to Data

Retrieving and Managing Data

Searching and Analyzing Data

Adding and Modifying Data

Copying and Transferring Data

Maintaining Database Integrity

Binding Data to .NET User Interfaces

Working with XML

Optimizing .NET Data Access

Enumerating and Maintaining Database Objects

Appendix A. Converting from C# to VB Syntax



ADO. NET Cookbook
ADO.NET 3.5 Cookbook (Cookbooks (OReilly))
ISBN: 0596101406
EAN: 2147483647
Year: 2002
Pages: 222
Authors: Bill Hamilton

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net