Walkthrough: Create a DataTable for .NET Framework applications using the Dataset Designer

Note

Datasets and related classes are legacy .NET Framework technologies from the early 2000s that enable applications to work with data in memory while the applications are disconnected from the database. They are especially useful for applications that enable users to modify data and persist the changes back to the database. Although datasets have proven to be a very successful technology, we recommend that new .NET applications use Entity Framework Core. Entity Framework provides a more natural way to work with tabular data as object models, and it has a simpler programming interface.

This walkthrough explains how to create a DataTable (without a TableAdapter) using the Dataset Designer. For information on creating data tables that include TableAdapters, see Create and configure TableAdapters.

Prerequisites

To complete this tutorial, you need the .NET desktop development and Data storage and processing workloads installed in Visual Studio. To install them, open Visual Studio Installer and choose Modify (or More > Modify) next to the version of Visual Studio you want to modify. See Modify Visual Studio.

Create a new Windows Forms application

  1. In Visual Studio, create a new Windows Forms App (.NET Framework) project and name it DataTableWalkthrough.

    The DataTableWalkthrough project is created and added to Solution Explorer.

Add a new Dataset to the application

  1. On the Project menu, select Add New Item.

    The Add New Item dialog box appears.

  2. In the left-hand pane, select Data, then select DataSet in the middle pane.

  3. Choose Add.

    Visual Studio adds a file called DataSet1.xsd to the project and opens it in the Dataset Designer.

Add a new DataTable to the Dataset

  1. Drag a DataTable from the DataSet tab of the Toolbox onto the Dataset Designer.

    A table named DataTable1 is added to the dataset.

  2. Click the title bar of DataTable1 and rename it Music.

Add columns to the DataTable

  1. Right-click the Music table. Point to Add, and then click Column.

  2. Name the column SongID.

  3. In the Properties window, set the DataType property to System.Int16.

  4. Repeat this process and add the following columns:

    SongTitle: System.String

    Artist: System.String

    Genre: System.String

Set the Primary Key for the table

All data tables should have a primary key. A primary key uniquely identifies a specific record in a data table.

To set the primary key, right-click the SongID column, and then click Set Primary Key. A key icon appears next to the SongID column.

Save Your Project

To save the DataTableWalkthrough project, on the File menu, select Save All.