Споделяне чрез


Add code to DataSets in n-tier applications

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can extend the functionality of a dataset by creating a partial class file for the dataset and adding code to it (instead of adding code to the DatasetName.Dataset.Designer file). Partial classes enable code for a specific class to be divided among multiple physical files. For more information, see Partial or Partial classes and methods.

The code that defines a dataset is generated every time changes are made to the dataset definition (in the typed dataset). This code is also generated when you make changes during the running of any wizard that modifies the configuration of a dataset. To prevent your code from being deleted during the regeneration of a dataset, add code to the dataset's partial class file.

By default, after you separate the dataset and TableAdapter code, the result is a discrete class file in each project. The original project has a file named DatasetName.Designer.vb (or DatasetName.Designer.cs) that contains the TableAdapter code. The project that's designated in the DataSet Project property has a file that's named DatasetName.DataSet.Designer.vb (or DatasetName.DataSet.Designer.cs).This file contains the dataset code.

Note

When you separate DataSets and TableAdapters (by setting the DataSet Project property), existing partial dataset classes in the project won't be moved automatically. Existing dataset partial classes must be moved manually to the dataset project.

Note

When validation code needs to be added, the typed dataset provides functionality for generating ColumnChanging and RowChanging event handlers. For more information, see Add validation to an n-tier dataset.

To add code to DataSets in n-tier applications

  1. Locate the project that contains the .xsd file.

  2. Select the .xsd file to open the dataset.

  3. Right-click the data table to which you want to add code (the table name in the title bar), and then select View Code.

    A partial class is created and opens in the Code Editor.

  4. Add code inside the partial class declaration.

    The following example shows where to add code to the CustomersDataTable in the NorthwindDataSet:

    Partial Public Class CustomersDataTable
        ' Add code here to add functionality
        ' to the CustomersDataTable.
    End Class
    
    partial class CustomersDataTable
    {
        // Add code here to add functionality
        // to the CustomersDataTable.
    }
    

See also