Compartilhar via


Add code to TableAdapters 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 TableAdapter by creating a partial class file for the TableAdapter 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 (Type).

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

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 named DatasetName.DataSet.Designer.vb (or DatasetName.DataSet.Designer.cs) that contains the dataset code.

Note

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

Note

The dataset provides functionality for generating ColumnChanging and RowChanging event handlers when validation is needed. For more information, see Add validation to an n-tier dataset.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in this article. You may be using a different edition of Visual Studio or different environment settings. For more information, see Personalize the IDE.

To add user code to a TableAdapter in an n-tier application

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

  2. Double click the .xsd file to open the Dataset Designer.

  3. Right-click the TableAdapter that you want to add code to, and then select View Code.

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

  4. Add code inside the partial class declaration.

  5. The following example shows where to add code to the CustomersTableAdapter in the NorthwindDataSet:

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

See also