Problem solve Get help with specific problems with your technologies, process and projects.

Building a typed dataset in Visual Studio

A procedure for building a typed dataset in Visual Studio.

A typed dataset is a class that derives from DataSet and provides type-safe access to tables and columns. The primary...

benefit of the typed dataset is that it allows you to access tables and columns by name rather than through collection-based methods. This excerpt from Informit explains how to build a typed dataset in Visual Studio.

Building a typed dataset to use in your project isn't especially difficult—it just takes a few steps. But it's well worth the effort. The steps are the same whether you're using Visual Basic or Visual C#.

  1. In Server Explorer, create a data connection to the Northwind database on an SQL Server.
  2. Create a new Windows Forms project.
  3. Drag and drop a SqlDataAdapter component onto the form. The Data Adapter Configuration Wizard appears. Click the Next button to continue.
  4. Select the Northwind data connection. Then click Next.
  5. The next screen asks how you want to query the data. You have three choices: create SQL statements, create new stored procedures, or use existing stored procedures. For this example we'll have it create new SQL statements. In a production program, you'd probably want to use stored procedures.
  6. The next screen allows you to tell the Wizard what data you want in the dataset. Click on the Query Builder button to display the Query Builder.
  7. For this example, we'll just grab the entire Employees table. Select the "Employees" table in the Add Table dialog box, and click on the Add button. Then close the Add Table dialog box.
  8. In the Employees table, select the "All Columns" checkbox. Your Query Builder should look like Figure 1.

    • Click the OK button to close the Query Builder.
    • Click the Next button, and the Wizard will generate the SQL statements.
    • Click Finish to apply the changes to your project.
    • Right-click on the SqlDataAdapter1 component on the form and select "Generate DataSet" from the popup menu. The Generate DataSet dialog will appear.
    • In this dialog box, make sure that the New radio button is selected and the Employees table is selected in the list of tables. Enter the name for your dataset. I called mine dsEmployees, as shown in Figure 2.
    • Click the OK button. Visual Studio will generate the typed dataset definition and add a dataset to your project called dsEmployees1.

      When you're all done with those steps, you should have three components showing in the non-visual components area in Visual Studio. Those components are a SqlDataAdapter, a SqlConnection, and the typed dataset, dsEmployees. In addition, Visual Studio will have added the file dsEmployees.xsd to the project. If you double-click on this file, you can see the XML scheme definition for your dataset. If you click on the "Show All Files" button at the top of the Solution Explorer and then expand the dsEmployees.xsd node, you'll see a file called dsEmployees.cs (or dsEmployees.vb). Double-click that file and you can peruse the code that Visual Studio generated for your dataset. You can also use the Class View or the Object Browser to examine the generated classes.

      Read more about typed datasets at Informit.

Dig Deeper on .NET development community