translation. This means that computers sitting behind these devices cannot be easily addressable
from outside the company network. In addition, as the number of public IPv4 addresses dwindles,
more connections are dynamically allocated an IP address. This makes hosting an application
within the company network that is publicly accessible almost impossible.
The Service Bus allows a service to be registered at a specific publicly addressable URL via the
service registry. Requests made to this URL are directed to the service via an existing outbound
connection made by the service itself. Working with the Service Bus can be as simple as changing
your existing WCF bindings across to the new relay bindings. As part of running your service it
registers with the service registry and initiates the outbound connection required for all further
communications.
access control service
Where an organization wants to integrate multiple cloud-based applications and/or an on-premise
application there needs to be some way of controlling who (authentication) has access to particular
resources (authorization). This is the function of the Access Control Service (ACS). Though still
in its infancy, the ACS is capable of verifying a user’s identity through the validation of input
claims, performing claims translation, and the supply of output claims for specific applications. For
example, you could sign into an application providing your e-mail address and a password. These
546 .
chaPter 25 WindoWS Azure
input claims would be used to authenticate you, as well as determine that you belong in the fancy-
hat group in application xyz that you are trying to access. The output claims may consist of your
e-mail address and the fancy-hat group. Note that because there is a previously established trust
relationship between application xyz and ACS (validated through signing of the output claims),
application xyz can trust the output claims.
suMMary
In this chapter you learned about the Windows Azure Platform and how it represents Microsoft’s
entry into the cloud computing space. Using Visual Studio 2010, you can adapt an existing, or
create a new, application or service for hosting in the cloud. The local Development Storage and
Fabric provide a great local testing solution, which means when you publish your application to
Windows Azure you can be confident that it will work without major issues.
Even if you don’t want to migrate your entire application into the cloud, you can use SQL Azure and
the AppFabric offerings to host your data, address connectivity challenges, or unify your application
security.
PART VI
data
. chaPter 26: Visual Database Tools
. chaPter 27: Datasets and Data Binding
. chaPter 28: Language Integrated Queries (LINQ)
. chaPter 29: The ADO NET Entity Framework
. chaPter 30: Reporting
26
Visual Database Tools
what’s in this chaPter?
.
Understanding the data-oriented tool windows within
Visual Studio 2010
.
Creating and designing databases
.
Navigating your data sources
.
Entering and previewing data using Visual Studio 2010
Database connectivity is essential in almost every application you create, regardless of whether
it’s a Windows-based program or a web site or service. When Visual Studio .NET was first
introduced, it provided developers with a great set of options to navigate to the database files
on their file systems and local servers, with a Server Explorer, data controls, and data-bound
components. The underlying .NET Framework included ADO.NET, a retooled database engine
that is more suited to the way applications are built today.
Visual Studio 2010 took those features and smoothed out the kinks, adding tools and
functionality to the IDE to give you more direct access to the data in your application. This
chapter looks at how you can create, manage, and consume data using the various tool
windows provided in Visual Studio 2010. These can be collectively referred to as the Visual
Database Tools.
database windows in Visual studio 2010
A number of windows specifically deal with databases and their components. From the
Data Sources window that shows project-related data files and the Data Connections
node in the Server Explorer, to the Database Diagram Editor and the visual designer for
database schemas, you’ll find most of what you need directly within the IDE. In fact, it’s
unlikely that you’ll need to venture outside of Visual Studio to work with your data.
550 .
chaPter 26 ViSuAl dATAbASe ToolS
Figure 26-1 shows the Visual Studio 2010 IDE with a current database-editing session. Notice how
the windows, toolbars, and menus all update to match the particular context of editing a database
table. In the main area is the list of columns belonging to the table, a Table Designer menu has been
added, and there is a Column Properties editing region below. The normal Properties tool window
contains the properties for the current table. The next few pages take a look at each of these
windows and describe their purposes so you can use them effectively.
fiGure 26-1
server explorer
In Chapter 12, you saw how the Server Explorer can be used to navigate the components that make
up your system (or indeed the components of any server to which you can connect). One component
of this tool window that was omitted from that discussion is the Data Connections node. Through
this node, Visual Studio 2010 provides a significant subset of the functionality that is available
through other products, such as SQL Server Management Studio, for creating and modifying
databases.
Figure 26-1 shows the Server Explorer window with an active database connection
(AdventureWorksLT2009_Data.mdf) and another database that Visual Studio is not currently
connected to (CRM.mdf). The database icon displays whether or not you are actively connected
to the database, and contains a number of child nodes dealing with the typical components of a
modern database, such as Tables, Views, and Stored Procedures. Expanding these nodes lists the
specific database components along with their details. For example, the Tables node contains a
Database Windows in Visual studio 2010 .
551
node for the Customer table, which in turn has nodes for each of the columns, such as CustomerID,
FirstName, and LastName. Clicking these nodes enables you to quickly view the properties within
the Properties tool window. This is the default database view; you can switch to either Object Type
or Schema view by selecting Change View, followed by the view to change to, from the right-click
context menu off the database node. Each of these views simply groups the information about the
database in a different hierarchy.
To add a new database connection to the Server Explorer window, click the Connect to Database
button at the top of the Server Explorer (third icon in from the left), or right-click the Data
Connections root node and select the Add Connection command from the context menu.
If this is the first time you have added a connection, Visual Studio asks you what type of data
source you are connecting to. Visual Studio 2010 comes packaged with a number of Data Source
connectors, including Access, SQL Server, and Oracle, as well as a generic ODBC driver. It also
includes a data source connector for Microsoft SQL Server Database File and Microsoft SQL Server
Compact databases.
The Database File option was introduced in SQL Server 2005 and borrows from the easy
deployment model of its lesser cousins, Microsoft Access and MSDE. With SQL Server Database
File, you can create a flat file for an individual database. This means you don’t need to attach it to
a SQL Server instance, and it’s highly portable — you simply deliver the .mdf file containing the
database along with your application. Alternatively, using a SQL Server Compact (SSC) database
can significantly reduce the system requirements for your application. Instead of requiring an
instance of SQL Server to be installed, the SSC runtime
can be deployed alongside your application.
Once you’ve chosen the data source type to use, the
Add Connection dialog appears. Figure 26-2 shows this
dialog for a SQL Server Database File connection, with
the settings that are appropriate to that data source type.
You are taken directly to this dialog if you already have
data connections defined in Visual Studio.
The Change button takes you to the Data Sources
page, enabling you to add different types of database
connections to your Visual Studio session. Note how
easy it is to create a SQL Server Database File. Just type
or browse to the location where you want the file and
specify the database name for a new database. If you
want to connect to an existing database, use the Browse
button to locate it on the file system.
Generally, the only other task you need to perform is to
specify whether your SQL Server configuration is using
Windows or SQL Server Authentication. The default
installation of Visual Studio 2010 includes an installation of SQL Server 2005 Express, which uses
Windows Authentication as its base authentication model.
fiGure 26-2
552 .
chaPter 26 ViSuAl dATAbASe ToolS
The Test Connection button displays an error message if you try to connect to
a new database. This is because it doesn’t exist until you click OK, so there’s
nothing to connect to!
When you click OK, Visual Studio attempts to connect to the database. If successful, it adds it to
the Data Connections node, including the children nodes for the main data types in the database,
as discussed earlier. Alternatively, if the database doesn’t exist, Visual Studio prompts you asking
if it should go ahead and create it. You can also create a new database by selecting the Create New
SQL Server Database item from the right-click menu off the Data Connections node in the Server
Explorer.
Table editing
The easiest way to edit a table in the database is to
double-click its entry in the Server Explorer. An editing
window is then displayed in the main workspace,
consisting of two components. The top section is
where you specify each field name, data type, and key
information such as length for text fields, and whether
the field is nullable.
Right-clicking a field gives you access to a set of
commands that you can perform against that field, as
shown in Figure 26-3 . This context menu contains the
same items as the Table Designer menu that is displayed
while you’re editing a table, but it is usually easier to use
the context menu because you can clearly see which field
you’ re modifying.
The lower half of the table editing workspace contains
the Column Properties window for the currently
selected column. Unlike the grid area that simply
lists the Column Name, Data Type, and whether
the column allows nulls, the column properties area
allows you to specify all of the available properties
for the particular Data Source type.
Figure 26-4 shows a sample Column Properties window
for a field, CustomerID, that has been defined with an
identity clause that is automatically incremented by 1
for each new record added to the table.
relationship editing
Most databases that are likely to be used by your
.NET solutions are relational in nature, which means
fiGure 26-3
fiGure 26-4
Database Windows in Visual studio 2010 .
553
you connect tables together by defining
relationships. To create a relationship, open
one of the tables that will be part of the
relationship and click the Relationships
button on the toolbar, or use the Table
Designer . Relationships menu command.
The Foreign Key Relationships dialog is
displayed (see Figure 26-5), containing
any existing relationships that are bound
to the table you selected.
Click the Add button to create a new
relationship, or select one of the existing
relationships to edit. Locate the Tables and
Columns Specification entry in the property
grid and click its associated ellipsis to set the
tables and columns that should connect to
each other. In the Tables and Columns dialog,
shown in Figure 26-6, first choose which
table contains the primary key to which
the table you selected will connect. Note that
for new relationships the Foreign key table
field is populated with the current table name
and cannot be changed.
Once you have the Primary key table, you then connect the fields in each table that should bind to
each other. You can add multiple fields to the relationship by clicking the blank row that is added as
you add the previous field. When you are satisfied with the relationship settings, click OK to save it
and return to the Foreign Key Relationships dialog.
Views
Views are predefined queries that can appear like tables to your application and can be made up of
multiple tables. Use the Data . Add New . View menu command or right-click the Views node in
Server Explorer and choose Add New View from the context menu.
The first task is to choose which tables, other views, functions, and synonyms will be included in
the current view. When you’ve chosen which components will be added, the View editor window is
displayed (see Figure 26-7). This editor should be familiar to anyone who has worked with a visual
database designer such as Access. The tables and other components are visible in the top area,
where you can select the fields you want included. The top area also shows connections between
any functions and tables. The View in Figure 26-7 connects three tables by linking all rows in the
Customer table with the CustomerAddress and Address tables. Figure 26-7 also shows that by
right-clicking the connector between tables you can change the type of join used. If you need to
add additional tables, right-click the design surface and select Add Table.
fiGure 26-5
fiGure 26-6
554 .
chaPter 26 ViSuAl dATAbASe ToolS
fiGure 26-7
The middle area shows a tabular representation of your current selection, and adds columns for