饭饭TXT > 学习管理 > 《Visual Studio 2010 高级编程(英文出书版)》作者:Nick Randolph/等【完结】 > [Visual.Studio.2010.高级编程].Professional.Visual.Studio.2010.txt

第 95 页

作者:Nick Randolph/等 当前章节:15456 字 更新时间:2026-6-18 14:51

being queried:

Vb

Dim qry = From c In context.Customers

.Include("CustomerAddresses")

.Include("CustomerAddresses.Address")

Where c.CustomerID = 1

Select c

Dim customer As Customer = qry.FirstOrDefault()

c#

var qry = from c in context.Customers

.Include("CustomerAddresses")

.Include("CustomerAddresses.Address")

where c.CustomerID == 1

select c;

Customer customer = qry.FirstOrDefault();

adVanced functionality

There’s too much functionality available in the Entity Framework to discuss in detail in this chapter,

but here’s an overview of some of the more notable advanced features available that you can

investigate further if you wish.

updating a database from an entity Model

As mentioned earlier, it’s possible with the Entity Framework to create an entity model from scratch,

and then have the Entity Framework create a database according to your model. Alternatively, you

summary .

643

can start with an existing database, but then get the Entity Framework to update the structure of

your database based on the new entities/properties/associations that you’ve added to your entity

model. To update the structure of the database based on additions to your model, you can use

the Generate Database Wizard by right-clicking the designer surface and selecting the Generate

Database from Model menu item.

adding business logic to entities

Though you are fundamentally building a data model with the Entity Framework rather than

business objects, it is possible to add business logic to your entities. The entities generated by the

Entity Framework are partial classes, enabling to you extend them and add your own code. This

code may respond to various events on the entity, or it may add methods to your entity that the

client application can use to perform specific tasks or actions.

For example, you might want to have the Product entity in your AdventureWorksLT entity model

automatically assign the value of the SellEndDate property when the SellStartDate property

is set (only if the SellEndDate property does not have a value). Alternatively, you may have some

validation logic or business logic that you want to execute when the entity is being saved.

Each property on the entity has two partial methods that you can extend: a Changing method

(before the property is changed) and a Changed method (after the property is changed). You can

extend these partial methods in your partial class to respond accordingly to the value of a property

being changed.

Plain old clr objects (Poco)

One of the big complaints with the first version of the Entity Framework was that your entities had

to inherit from EntityObject (or implement a set of given interfaces), meaning that they had a

dependency on the Entity Framework — which made them unfriendly for use in projects where test-

driven development (TDD) and domain-driven design (DDD) practices were employed. In addition,

many developers wanted their classes to be persistence ignorant — that is, contain no logic or

awareness of how they were persisted.

By default, the entities generated from the Entity Model Data Wizard in the Entity Framework v4

still inherit from EntityObject, but you now have the ability to use your own classes that do not

need to inherit from EntityObject or implement any Entity Framework interfaces, and whose

design is completely under your control. These types of classes are often termed Plain Old CLR

Objects, or POCO for short.

suMMary

In this chapter you learned that the Entity Framework is an Object Relational Mapper (ORM) that

enables you to create a conceptual model of your database in order to interact with databases in

a more productive and maintainable manner. You then went on to learn how to create an entity

model, and how to write queries against it in code.

330

reporting

what’s in this chaPter?

.

Designing Reports

.

Generating Reports

.

Deploying Reports

One of the key components of almost every business application is reporting. Businesses

put data into the system in order to get useful information out of it, and this information is

generally in the form of reports. Numerous reporting tools and engines are available, and it

can often be hard to choose which one is best for your application or system (they tend to

work in different ways and have different pros/cons).

Visual Studio 2010 contains a built-in report designer that saves to files using the RDL file

specification — and reports built using this designer can be generated using the local report

engine, or rendered on a remote report server running SQL Server Reporting Services.

The professional versions of Visual Studio 2010 (and higher) also come with another well-

known reporting tool called Crystal Reports. However, this chapter specifically looks at

Visual Studio’s report designer, and how to use it to design and generate reports (using the

local report engine).

GettinG started with rePortinG

When you start designing reports, you will either want to add a report to an existing project

or start a completely new project (such as for a reporting application). If it is the latter, the

easiest way to get started is to create a new project using the Reports Application project

template. This creates a Windows Forms project already set up with the necessary assembly

references, a form with the Report Viewer control on it, and an empty report. Let’s look at the

former scenario and how to manually get started (which really isn’t much extra work).

646 .

chaPter 30 reporTing

Reports can be viewed in either a Windows Forms application or an ASP.NET application using the

Report Viewer control. There are two Report Viewer controls — one for use in web projects and

one for use in Windows Forms projects, and both are almost identical in appearance and how you

use them to render reports.

To render reports in a WPF application, you can use the Windows Forms

interoperability feature detailed in Chapter 18 and use the Windows Forms control

(because there is no Report Viewer control in WPF). Displaying reports

in Silverlight applications is a bit harder because Silverlight has no Report

Viewer control either (nor support for printing). In this case it is probably best

to render reports to PDF, stream them through to the client using a HTTP

handler, and display them in a different browser window.

Now you need to add some assembly references to your project that are required for using the Report

Viewer control and the report engine. If you are working with an ASP.NET project you will need to

add a reference to Microsoft.Reporting.WebForms.dll, or if you are working with a Windows

Forms project you will need to add a reference to Microsoft.Reporting.WinForms.dll. Alternatively,

the Report Viewer control should be in your Toolbox for both project types, and dropping it onto your

report will automatically add the required assembly reference to your project.

Now add a report definition file to your project. Add a new item to your project, and select the

Reporting subsection as shown in Figure 30-1.

fiGure 30-1

Designing reports .

647

Two items here are of interest: the Report item and the Report Wizard item. Selecting the Report

item creates an empty report definition file — essentially a blank slate that you can start working

with. Selecting the Report Wizard item creates a report definition file and automatically starts the

Report Wizard (detailed later in this chapter), which will design a report layout for you based upon

your choices. You will generally want to start your report by using the Report Wizard, and then

modify its output to suit your requirements.

Before you get into designing the report, it is important to clarify the different parts of a reporting

system, the terms you use when you reference each, and how they hang together (because this can be

somewhat confusing initially). There are six main parts:

.

Report Designer

.Report Definition File

.

Data Sources

.Reporting Engine

. Report

.

Report Viewer

You use the report designer to design the report definition file (at design time), creating its structure

and specifying the various rules of how the report will be laid out. At run time, you pass the report

definition file and one or more data sources to the reporting engine. The reporting engine uses the

two to generate the report, which it then renders in the Report Viewer (or a specified alternative

output format such as PDF).

Where this can become confusing is that the Report Viewer is the local report

engine. So you are passing the report definition file and the data sources to the

Report Viewer and it then both renders and displays it. From a conceptual

perspective, however, it’s probably best to think of these as separate components

and it will make more sense.

desiGninG rePorts

Take a look now at how to go about designing a report. You will look at the manual process of

designing a report, and then later take a look at how the Report Wizard automates the design

process. For now, you will be working with an empty report that was created by adding a new item

to the project and using the Report item template. When you create this item it will immediately be

opened in the report designer as shown in Figure 30-2.

648 .

chaPter 30 reporTing

fiGure 30-2

In the document area you have the design surface upon which you lay out the report. On the bottom

left is the Report Data tool window, which will contain the data fields that you can drag onto your

report. If you accidentally close this window you can open it again by using the View . Report

Data menu. Above it, the Toolbox window contains the controls that can be added to the report

surface. When you are working with the design surface of a report you will note that a Report menu

is also added to the menu bar.

Due to the nature of the local report engine, which can’t query data sources itself

(as discussed shortly), there unfortunately is no way to preview the report in the

designer. This means that in order to view the output of your report you must

have already set up a form with a Report Viewer control, and have written the

code that populates the data structures and initiates the rendering process. This

can make the report design process a little painful, and it is possibly worthwhile

creating a temporary project that makes it easy to test your report. You can find

the code required to do so later in this chapter.

defining data sources

Before you can design a report you need to start with a data source, because it is the data source

that will dictate a large portion of the report’s design. At design time the data sources won’t contain

any data, but the report needs the data sources for their structure.

An important concept to understand when starting with the local report engine is that you must

pass it the data when generating the report — it doesn’t query the data sources itself. The upside

of this is that the data can come from a wide variety of sources; all you need to be able to do is to

query the data, and you can then manipulate it and pass it to the report engine in a structure that

it understands. The main structures you can use to populate your report that the report engine will

understand include DataSets, objects, and Entity Framework entities.

Designing reports .

649

The server report engine (SQL Server Reporting Services) can query SQL Server

databases itself (and some other various data sources via OLEDB and ODBC),

and the query to obtain the data used by the report is stored in the report definition

file. You can spot report definition files that are for use by SQL Server Reporting

Services fairly easily because they will have an .rdl extension, whereas the files

for use by the local report engine have an .rdlc extension (the c stands for client-

side processing). It’s reasonably easy to convert reports from using the local report

engine to using SQL Server Reporting Services, because the underlying file formats

are based upon the same Report Definition Language (RDL). The reason you

might use SQL Server Reporting Services over the local report engine is to reduce

the load on your server (such as the web server), and offload that to a separate

server. Generating reports can be quite resource- and CPU-intensive, so you can

make your system a lot more scalable by delegating this task to another server.

SQL Server Reporting Services requires a full SQL Server license, but if you’re

using SQL Server Express Edition you can use a limited version of it if you install

the free SQL Server Express Edition with Advanced Services.

You can use an Entity Framework model for the data source for your report; however, a limitation

of the local report engine is that you can’t join data from separate data sources (in this case entities)

in the report, which is often required in reporting (unless you have imported views from your

database into your Entity Framework model that align with the requirements for your report).

Therefore, you will need to either create a Typed DataSet or create a class to populate with the

joined data, which you can then pass to the report engine.

As an example, you will simply be using the AdventureWorksLT Entity Framework model that you

created in Chapter 29 as the source of the data for this report. The first step is to add an entity from this

model as a data source for the report. To do so, click the New menu in the Report Data tool window,

and select the Dataset menu item. This displays the Dataset Properties window shown in Figure 30-3 .

fiGure 30-3

650 .

chaPter 30 reporTing

You should give the data source a meaningful name, because you will be referencing the data source

name in code when you are passing the local report engine the data to populate it with. Enter this name

in the Name textbox. Now you need to select the location of the data source from the Data Source drop-

down list. The data source will usually be in your project, so you can select it from the list.

目录
设置
设置
阅读主题
字体风格
雅黑 宋体 楷书 卡通
字体大小
适中 偏大 超大
保存设置
恢复默认
手机
手机阅读
扫码获取链接,使用浏览器打开
书架同步,随时随地,手机阅读
首 页 < 上一章 章节列表 下一章 > 尾 页