9.9. Improving Performance While Filling a DataSet
Problem
Given a DataSet containing many
related tables that takes a long time
to fill, you need to improve the performance.
Solution
Use the
EnforceConstraints
property of the DataSet and the
BeginLoadData( )
and EndLoadData( )
methods of the contained
DataTable objects to improve performance while
filling a complex DataSet.
The sample code contains one event handler and one method:
- Go
Button.Click Times the filling of the
DataSetcreated by theCreateDataSet( )method (described next). TheEnforceConstraintsproperty of theDataSetis set as specified and theBeginLoadData( )andEndLoadData( )methods of the containedDataTableobjects are used, if specified. ADataAdapteris used to fill a specifiedDataSetwith data from the Orders and Order Details tables in the Northwind database. Ten iterations are performed, and the total fill time is returned in ticks, which are 100-nanosecond intervals.CreateDataSet( )This method builds a
DataSetcontaining the table schema for the Orders and Order Details tables from the Northwind database and creates a data relation between the tables. TheDataSetis returned by the method.
The C# code is shown in Example 9-12.
Example 9-12. File: DataSetFillPerformanceForm.cs
// Namespaces, variables, and constants using System; using System.Configuration; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; // Table name constants public const String ORDERS_TABLE = "Orders"; public ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access