Edit

Share via


Extend the functionality of a TableAdapter in .NET Framework applications

Note

The DataSet class and related classes are legacy .NET Framework technologies from the early 2000s that enable applications to work with data in memory while the apps are disconnected from the database. The technologies are especially useful for apps that enable users to modify data and persist the changes back to the database. Although datasets are a proven successful technology, the recommended approach for new .NET applications is to use Entity Framework Core. Entity Framework provides a more natural way to work with tabular data as object models, and has a more simple programming interface.

You can extend the functionality of a TableAdapter by adding code to the TableAdapter's partial class file.

The code that defines a TableAdapter is regenerated when any changes are made to the TableAdapter in the Dataset Designer, or when a wizard modifies the configuration of a TableAdapter. To prevent your code from being deleted during the regeneration of a TableAdapter, add code to the TableAdapter's partial class file.

Partial classes allow code for a specific class to be divided among multiple physical files. For more information, see Partial or partial (type).

Locate TableAdapters in code

While TableAdapters are designed with the Dataset Designer, the TableAdapter classes that are generated are not nested classes of DataSet. TableAdapters are located in a namespace based on the name of the TableAdapter's associated dataset. For example, if your application contains a dataset named HRDataSet, the TableAdapters would be located in the HRDataSetTableAdapters namespace. (The naming convention follows this pattern: DatasetName + TableAdapters).

The following example assumes a TableAdapter named CustomersTableAdapter is in a project with NorthwindDataSet.

To create a partial class for a TableAdapter

  1. Add a new class to your project by going to the Project menu and selecting Add Class.

  2. Name the class CustomersTableAdapterExtended.

  3. Select Add.

  4. Replace the code with the correct namespace and partial class name for your project as follows:

    namespace NorthwindDataSetTableAdapters
    {
        public partial class CustomersTableAdapter
        {
            // Add user code here. For example:
            public override string ToString()
            {
                return "Overridden in the partial class.";
            }
        }
    }