Walkthrough: Create a Dataset with the Dataset Designer
Applies to: Visual Studio Visual 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
In this walkthrough you create a dataset using the Dataset Designer. The article takes you through the process of creating a new project and adding a new DataSet item to it. You'll learn how to create tables based on tables in a database without using a wizard.
Prerequisites
This walkthrough uses SQL Server Express LocalDB and the Northwind sample database.
If you don't have SQL Server Express LocalDB, install it either from the SQL Server Express download page, or through the Visual Studio Installer. In the Visual Studio Installer, SQL Server Express LocalDB can be installed as part of the Data storage and processing workload, or as an individual component.
Install the Northwind sample database by following these steps:
In Visual Studio, open the SQL Server Object Explorer window. (SQL Server Object Explorer is installed as part of the Data storage and processing workload in the Visual Studio Installer.) Expand the SQL Server node. Right-click on your LocalDB instance and select New Query.
A query editor window opens.
Copy the Northwind Transact-SQL script to your clipboard. This T-SQL script creates the Northwind database from scratch and populates it with data.
Paste the T-SQL script into the query editor, and then choose the Execute button.
After a short time, the query finishes executing and the Northwind database is created.
Create a New Windows Forms Application Project
In Visual Studio, on the File menu, select New > Project.
Expand either Visual C# or Visual Basic in the left-hand pane, then select Windows Desktop.
In the middle pane, select the Windows Forms App project type.
Name the project DatasetDesignerWalkthrough, and then choose OK.
Visual Studio adds the project to Solution Explorer and display a new form in the designer.
Add a New Dataset to the Application
On the Project menu, select Add New Item.
The Add New Item dialog box appears.
In the left-hand pane, select Data, then select DataSet in the middle pane.
Name the Dataset NorthwindDataset, and then choose Add.
Visual Studio adds a file called NorthwindDataset.xsd to the project and opens it in the Dataset Designer.
Create a Data Connection in Server Explorer
On the View menu, click Server Explorer.
In Server Explorer, click the Connect to Database button.
Create a connection to the Northwind sample database.
Create the Tables in the Dataset
This section explains how to add tables to the dataset.
To create the Customers table
Expand the data connection you created in Server Explorer, and then expand the Tables node.
Drag the Customers table from Server Explorer onto the Dataset Designer.
A Customers data table and CustomersTableAdapter are added to the dataset.
To create the Orders table
Drag the Orders table from Server Explorer onto the Dataset Designer.
An Orders data table, OrdersTableAdapter, and data relation between the Customers and Orders tables are added to the dataset.
To create the OrderDetails table
Drag the Order Details table from Server Explorer onto the Dataset Designer.
An Order Details data table, OrderDetailsTableAdapter, and a data relation between the Orders and OrderDetails tables are added to the dataset.
Next Steps
Save the dataset.
Select items in the Data Sources window and drag them onto a form. For more information, see Bind Windows Forms controls to data in Visual Studio.
Add more queries to the TableAdapters.
Add validation logic to the ColumnChanging or RowChanging events of the data tables in the dataset. For more information, see Validate data in datasets.