Data Models, Datasets, and the
ADO.NET Interface
I
n Chapters 1 and 2, you were introduced to client-side RS and report controls. I’m sure you
are eager to start putting this knowledge to the test by developing some reports now. Well, we
will do just that starting in Chapter 4. So, what are you going to learn in this chapter? Before
we move to the practical part of this book, let me introduce you to few key elements in the
overall process of report development. We know the importance of data in producing reports.
In this chapter, we will look at the process of gathering data and working with our
RealWorld
database.
This chapter will cover
• Working with a real-world sample database (
RealWorld)
• Gathering data into a dataset
• The basics of the ADO.NET C# interface code to get the data into the dataset
Working with a Real-World Sample Database
I’m sure by this time you already have downloaded the code from the Apress site according
to the instructions provided in this book’s Introduction. If not, then I’d strongly advise you
to do so before proceeding further. If you look at the downloaded code, you’ll see two files,
RealWorld.mdf and RealWorld.ldf, inside the database folder
. What are these files? The
.mdf
file holds the data, and .ldf file holds the log information. Typically, a SQL Server database
is made of an
.mdf and an .ldf file. Therefore, our real-world database also has one of each
of these files.
Understanding the Data Model
Let me ask you something. What is a real-world data model, and why do you need to know this
to continue using this book and developing reporting projects? As you know, getting to know
the sour
ce of the data is an impor
tant step in the r
eport development process. Therefore, we
must kno
w which data model w
e ar
e going to use
.
T
o put it simply
, a data model helps us to
understand the internals of how data is stored inside a database.
Whether y
ou ar
e a beginner or an exper
t in understanding data models, I’m confident
that y
ou will find this book
’
s r
eal-world data model simple and r
ichly div
erse
. Richly diverse
51
CHAPTER 3
8547ch03final.qxd 8/30/07 4:10 PM Page 51
sounds interesting, doesn’t it? Why not! How often do you find a data model that has entities
b
elonging to several different business cases? For example, in the
R
ealWorld
d
atabase, you
have an Itinerary table that holds travel-related data and another table that holds product
pricing information for inventory. The simple reason for the diversity of information in this
data model is my intention to cover as many different business cases as possible in the report-
ing projects coming in the following chapters.
To keep things simple, you’ll find that a majority of the reporting projects in the book
gather data from a single dedicated table in the
RealWorld database. For example, the report-
ing project showing you how to create a Travel Itinerary report will use the data from the
Itinerary table inside the RealWorld database. You don’t have to worry about how data gets
inside the table. Our goal here is to get the data out and publish it with RS.
Attaching the RealWorld Database
There are two common ways a database is distributed, as a backup or as physical files. In the
case of a backup, you need to restore it. If a database is distributed as physical files, you need
to attach it. I find attaching database files much simpler, especially if you’re a new to data-
bases—hence the
.mdf and .ldf files in the downloaded code of the RealWorld database.
So, let’s attach the database. As you know, the SQL Server database is needed for this
book; it can be one of the full versions or the free express edition. Full versions of SQL Server
come with the SQL Server Management Studio tool to manage the database. If you have the
express edition of SQL Server, I’d suggest you download SQL Server Management Studio
Express from here:
/>➥
C243A5AE-4BD1-4E3D-94B8-5A0F62BF7796&displaylang=en
By the time this book reaches you, SQL Server 2008 may be on the market. If this is the
case, please download the relevant tool from the download section of Microsoft’s web site.
Attaching the database is a simple process. Once you make sure you have SQL Server
Management Studio installed, please open it by accessing it from your Windows Start button,
and use the following steps to attach the database; the instructions are the same for both the
full and express releases of SQL Server 2005:
1. F
rom the Connect to Server dialog box, select the proper credentials to connect to the
server. I’m connecting using Windows authentication to my local instance of SQL Sever
(
localhost); see Figure 3-1. Please select the appropriate connection according to your
SQL Server setup.
CHAPTER 3
■
DATA MODELS, DATASETS, AND THE ADO.NET INTERFACE52
8547ch03final.qxd 8/30/07 4:10 PM Page 52
Figure 3-1. Connecting to a SQL Server instance
2. In Object Explorer, right-click the Databases node, and select Attach (see Figure 3-2).
Figure 3-2. Select the Databases node and the Attach menu option.
CHAPTER 3
■
DATA MODELS, DATASETS, AND THE ADO.NET INTERFACE 53
8547ch03final.qxd 8/30/07 4:10 PM Page 53
3. You need to complete the following four steps in the Attach Database dialog box;
p
lease see Figure 3-3 for an illustration of the steps:
a. Click the Add button.
b. This action will open the Locate Database Files dialog; browse to the location of
the
RealWorld.mdf file in your PC.
c. Select the file, and click the OK button.
d. You’ll see that RealWorld.mdf is selected and ready to attach; click the OK button to
compete the process.
Figure 3-3. Steps to browse and select RealWorld.mdf to attach
This action will add a new node to the database tree on your SQL Server instance. Please
make sure your database appears under the Databases tree node (see Figure 3-4). Now, you’re
all set to start reporting on your database. All right, with our database attached, let’s move on
now to learn some essentials of datasets.
CHAPTER 3
■
DATA MODELS, DATASETS, AND THE ADO.NET INTERFACE54
8547ch03final.qxd 8/30/07 4:10 PM Page 54
Figure 3-4. Database node after attaching RealWorld sample database
Gathering Data into a Dataset
As I explained to you in the Introduction, we have to gather data and store it temporarily for
the RS reporting engine to use it. This temporary placeholder for our gathered data is called a
dataset. For each report you develop, it is your responsibility as a developer to create a dataset.
What Is a Dataset?
Before we look into different ways to create a dataset, let me offer a quick insight into datasets!
In simple words, a dataset is like a miniature relational database in memory, with tables, rela-
tionships, and constraints. A dataset is a very powerful component of the ADO.NET architec-
tur
e
. An in-depth analysis of a dataset is bey
ond the scope of this book; ther
efore, I’ll discuss
only the part that is required for us to know to develop reports.
■
Note
Y
ou can learn
more on da
tasets here:
/>system.data.dataset(VS.71).aspx
.
What should y
ou kno
w about datasets?
Well, all you have to know is how to create a
dataset and a data table inside the dataset. Starting in Chapter 4, you’ll notice that each
reporting project makes use of a dataset. The data table inside the dataset is identical in
CHAPTER 3
■
DATA MODELS, DATASETS, AND THE ADO.NET INTERFACE 55
8547ch03final.qxd 8/30/07 4:10 PM Page 55
structure to the underlying source table from the database. A dataset like this is also com-
m
only known as a typed dataset.
We create a typed dataset by deriving it from the base dataset class. It has all the function-
ality that the base dataset class has to offer. In addition to this, a typed dataset provides
strongly typed methods, events, and properties. To put it simply, this means you can access
resources inside the dataset, such as tables and columns, by name. Another important benefit
of a typed dataset is that any type mismatch is caught during the compilation, rather than at
run time.
Creating a Typed Dataset Manually
This is an important section, as it explains how we are going to create the datasets for the rest
of our reporting examples in this book. As you know, we are using different clients to report
on, but no matter what the client, the process to add a dataset remains the same. For example,
if you are creating a Windows Forms client or a Web Service client, you follow the same steps
to add a dataset.
Let’s make use of Windows Forms to create our example dataset. Before we go into creat-
ing an actual dataset, let’s get our Windows Forms client ready. If you are new to the Visual
Studio IDE, I’d advise you to go through this book’s Appendix for a quick introduction.
Please open Visual Studio, and use the following steps, illustrated in Figure 3-5, to cre-
ate a Windows application project:
1. Click File
➤
New
➤
Project, or press the hot keys Ctrl+Shift+N.
2. In the “Project types” pane of the New Project dialog box, select Visual C#
➤
Windows.
3. In the Templates pane, select Windows Application.
4. Please give the application a name; I’ve called the project TypedDataset. You may
choose a different location for storing the application files according to your prefer-
ence.
5. Click the OK button to finish the process. After you click OK, Visual Studio will create a
new Windows application project. You’ll also notice that a new form with the name
Form1 is part of the project.
CHAPTER 3
■
DATA MODELS, DATASETS, AND THE ADO.NET INTERFACE56
8547ch03final.qxd 8/30/07 4:10 PM Page 56
Figure 3-5. Steps to add a new Windows Forms project
Perfect! We have the Windows Forms project created now. Let’s move on and see how a
dataset is created with the VS IDE. Let’s assume that you have a
CreditLimit table in a SQL
Server database with three columns inside:
CustomerID, CustomerName, and LimitAmount. Now,
our goal is to create a typed dataset with a data table inside that has a similar structure to the
CreditLimit table.
What you are going to see now is the common method I’ll use throughout the book to cre-
ate the dataset. While trying any practical report from this book, if you face any difficulty in
creating a dataset or data table, I’d advise you to revisit this section.
Adding a dataset is easy; here is how you can add a dataset to any client you will develop
with the
V
isual S
tudio IDE. S
tart by selecting the project
TypedDataset in S
olution Explor
er
.
Right-click it, and select Add
➤
New Item. Please see Figure 3-6 for an illustration of the steps.
CHAPTER 3
■
DATA MODELS, DATASETS, AND THE ADO.NET INTERFACE 57
8547ch03final.qxd 8/30/07 4:10 PM Page 57
Figure 3-6. Steps to add a new item to the project
After finishing the steps illustrated in Figure 3-6, you’ll be presented with a dialog box to
pick a new item from various available templates. Please select DataSet as your choice, and
name it
dsCreditLimit; see Figure 3-7.
Figure 3-7. S
teps to add a new dataset to the project
CHAPTER 3
■
DATA MODELS, DATASETS, AND THE ADO.NET INTERFACE58
8547ch03final.qxd 8/30/07 4:10 PM Page 58
After you go through all the steps shown Figures 3-6 and 3-7, you’ll notice that a new
d
ataset is added to the project and a blank dataset designer is open for you to add a data table
(see Figure 3-8).
Figure 3-8. Dataset design surface
Creating a Data Table
Now that we’ve created the dataset, let’s move on to add a data table to it. Use the following
steps to add a data table inside the dataset:
1. You can go to the dataset designer in two ways: double-click dsCreditLimit inside
Solution Explorer, or right-click the
dsCreditLimit node and select View Designer
(see Figure 3-9).
Figure 3-9. S
teps to launch the DataSet design surface
2. Let’s add the data table by right-clicking the design surface and selecting Add
➤
DataTable (see Figure 3-10).
CHAPTER 3
■
DATA MODELS, DATASETS, AND THE ADO.NET INTERFACE 59
8547ch03final.qxd 8/30/07 4:10 PM Page 59