Danh mục

Creating a Strongly Typed DataSet

Số trang: 5      Loại file: pdf      Dung lượng: 34.46 KB      Lượt xem: 2      Lượt tải: 0    
thaipvcb

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

[ Team LiB ] Recipe 2.3 Creating a Strongly Typed DataSet Problem You want to create a strongly typed object wrapper around a DataSet. Solution Use one of the three techniques shown in the discussion to create a strongly typed DataSet using either the Visual Studio .NET IDE or a command line approach.
Nội dung trích xuất từ tài liệu:
Creating a Strongly Typed DataSet [ Team LiB ]Recipe 2.3 Creating a Strongly Typed DataSetProblemYou want to create a strongly typed object wrapper around a DataSet.SolutionUse one of the three techniques shown in the discussion to create a strongly typedDataSet using either the Visual Studio .NET IDE or a command line approach.DiscussionA strongly typed DataSet is a collection of classes that inherit from and extend theDataSet, DataTable, and DataRow classes, and provide additional properties, methods,and events based on the DataSet schema. You can use all of the functionality in classesfrom which the strongly typed classes inherit in the same way as with untyped classes.A strongly typed DataSet class contains, in addition to a single class extending theDataSet class, three classes for each table in the DataSet extending each of the DataTable,DataRow, and DataRowChangeEvent classes. This recipe describes these classes anddiscusses their commonly used methods and properties.There is a class named TableNameDataTable for each table in the strongly typed DataSet.It has the base class DataTable. Table 2-1 lists commonly used methods of this classspecific to the strongly typed DataSet. Table 2-1. TableNameDataTable methods Method Description Adds a row to the table. The method has two overloads: one takes a TableNameRow object as theAddTableNameRow( ) argument, while the other takes a set of arguments containing the column values. Takes N arguments which are the values of theFindByPrimaryKeyField1 ... primary key fields of the row to find. Returns aPrimaryKeyFieldN( ) TableNameRow object, if found.NewTableNameRow( ) Takes no arguments and returns a new TableNameRow object with the same schema as the table to be used for adding new rows to the table in the strongly typed DataSet.There is a class named TableNameRow for each table in the strongly typed DataSet. Ithas the base class DataRow and represents a row of data in the table. Table 2-2 listscommonly used properties and methods of this class specific to the strongly typedDataSet. Table 2-2. TableNameRow class properties and methods Property/method Description Sets and get the value of a column. The typed accessor isTyped Accessor exposed as a property having the same name as the underlying data column. Returns a Boolean value indicating whether the fieldIsColumnNameNull( ) contains a null value.SetColumnNameNull( ) Sets the value of the underlying field to a null value.GetChildTableNameRows( Returns the rows for the table as an array of) ChildTableNameRow objects. Returns the parent row as an object of typeParentTableNameRow( ) ParentTableNameRow.There is a class named TableNameRowChangeEvent for each table in the strongly typedDataSet. It has the base class EventArgs. Table 2-3 describes the properties of this class. Table 2-3. TableNameRowChangeEvent propertiesProperty Description A value from the System.Data.DataRowAction enumeration that describesAction the action performed on a row that caused the event to be raised.Row The TableNameRow object for which the event was raised.A strongly typed DataSet has some advantages over using an untyped DataSet: • The schema information is contained within the strongly typed DataSet resulting in a performance over retrieving schema information at runtime. The schema of an untyped DataSet can also be defined programmatically, as discussed in Recipe 2.1, resulting in similar performance. • Programming is more intuitive and code is easier to maintain. Table, column, and other object names are accessed through properties having names based on the underlying data source object names rather than by using index or delimited string arguments. The Visual Studio .NET IDE provides autocomplete functionality for strongly typed DataSet names. • Type mismatch errors and errors resulting from misspelled or out of bounds arguments used with DataSet objects can be detected during compilation, rather than at runtime.The disadvantages of a strongly typed DataSet object include: • Additional overhead when executing. If strongly typed functionality is not required, performance is better with an untyped DataSet rather than with a typed DataSet. • A strongly typed DataSet must be regenerated when the structure of the underlying data source changes. Applications using these strongly typed DataSet objects will need to be rebuilt with a reference to the new strongly typed DataSet. With an untyped DataSet, as long as the new schema is a superset of the old schema, existing clients can simply ignore any new data and do not need to be recompiled.Four ways to generate a typed DataSet class are described in the following subsections.Using the Visual Studio .NET IDE to generate a typed DataSetThe first and easiest method uses Visual Studio .NET following these steps: 1. Drop a DataAdapter object from the Data tab in the Visual Studio .NET Toolbox onto a design surface such as a form or a component. The Data Ad ...

Tài liệu được xem nhiều: