Databases Reference
In-Depth Information
Working with Datasets and Data Adapters
The dataset constructor is overloaded.
DataSet ds = new DataSet();
DataSet ds = new DataSet("MyDataSet");
If you use the parameterless constructor, the dataset name defaults to NewDataSet . If
you need more than one dataset, it's good practice to use the other constructor and name
it explicitly. However, you can always change the dataset name by setting its DataSetName
property.
You can populate a dataset in several ways, including the following:
• Using a data adapter
• Reading from an XML document
In this chapter, we'll use data adapters. However, in the “Using Datasets and XML”
section, you'll take a quick peek at the converse of the second method, and you'll write
from a dataset to an XML document.
Try It Out: Populating a Dataset with a Data Adapter
In this example, you'll create a dataset, populate it with a data adapter, and then display
its contents.
1. Create a new Console Application project named Chapter13. When Solution
Explorer opens, save the solution.
2. Rename the Chapter13 project to PopDataset. Rename the Program.cs file to
PopDataset.cs , and replace the generated code with the code in Listing 13-1.
Listing 13-1. PopDataSet.cs
using System;
using System.Data;
using System.Data.SqlClient;
Search WWH ::




Custom Search