Tải bản đầy đủ (.pdf) (13 trang)

Using LINQ to SQL

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (318.16 KB, 13 trang )

Chapter 20
Using LINQ to SQL
After completing this chapter, you will be able to:

Build LINQ queries that use the LINQ to SQL provider

Understand how LINQ to SQL prepares queries for processing

Determine when to use LINQ to SQL over LINQ to Entities
LINQ is an extensible system, enabling a consistent querying experience against different
data platforms. Sometimes these different systems overlap, providing access to the same
target platform through different providers. SQL Server is one such database platform. LINQ
to DataSet and LINQ to Entities both allow you to build LINQ queries that interact with data
sourced from SQL Server, either directly (LINQ to Entities) or indirectly (LINQ to DataSet).
LINQ to SQL, also included as a native LINQ provider within the .NET Framework, provides a
third option for processing SQL Server data.
This chapter focuses on the LINQ to SQL provider and the benefits it supplies to your data-
focused application. In many ways, LINQ to SQL feels like LINQ to Entities, especially when
using its class-generation feature. However, LINQ to SQL was built specifically to interact
with SQL Server database tables, and its queries reflect that closer relationship.
Note
In October 2008, soon after the announcement of its plans for Visual Studio 10 and the
related .NET Framework 4.0 release, Microsoft provided guidance on the future of its LINQ to
SQL provider. This “Update on LINQ to SQL and LINQ to Entities Roadmap” blog entry posted
by the ADO.NET team ( />sql-and-linq-to-entities-roadmap.aspx) indicated that the Entity Framework would be the “rec-
ommended data access solution for LINQ to relational scenarios.” The posting also included a
commitment to evolve the LINQ to SQL product based on customer feedback.
Note
The exercises in this chapter all use the same sample project, a tool that makes queries
using LINQ to SQL. Although you will be able to run the application after each exercise, the ex-
pected results for the full application might not appear until you complete all exercises in the


chapter.
Dwonloaded from: iDATA.ws
332
Understanding the LINQ to SQL Provider
LINQ to SQL is a LINQ provider that targets SQL Server databases. Its simple configuration
needs and its focus on the logical organization of the underlying tables make it a useful tool
for applications that need easy access to a specific SQL Server database.
Comparing LINQ to SQL with LINQ to Entities
The LINQ to SQL provider first appeared with the initial release of LINQ, part of Visual Studio
2008 and the .NET Framework 3.5. It preceded the release of LINQ to Entities, which was
delivered as part of the .NET Framework 3.5 SP1. The two platforms share many similarities,
including the following:

Modeling of data entities using an XML-based schema language

A Visual Studio–hosted visual designer that simplifies XML model creation

Generation of a language-specific class layer from the model

Support for database updates using custom stored procedures

Delayed query processing through LINQ-constructed SQL statements
Despite these similarities, the two systems diverge significantly in many respects. These dif-
ferences often help determine which of the two solutions is best for a given application. The
two systems differ in four key areas:

Platform support The Entity Framework and its LINQ to Entities extension include
support for a wide range of database platforms. In contrast, LINQ to SQL communicates
only with SQL Server 2000 and later, including SQL Server Compact 3.5.
Note

The visual designer used to set up LINQ to SQL models does not work with SQL Server
Compact 3.5.

Model focus LINQ to Entities queries focus on the Entity Framework conceptual
model, which is an abstraction of the underlying logical database model. This model
can differ significantly from the organization presented within the database. In LINQ to
SQL, the model closely reflects the database tables that support it.

Overhead LINQ to SQL is extremely lightweight compared with its Entity Framework
counterpart. EF’s three model layers allow for tremendous flexibility, but such design
comes at a performance and memory overhead cost.

Extensibility While the query features available in both LINQ to Entities and LINQ to
SQL are comparable, the Entity Framework’s design offers many opportunities for third-
party enhancement that aren’t currently possible with LINQ to SQL.
Dwonloaded from: iDATA.ws
Chapter 20 Using LINQ to SQL
333
Using LINQ to SQL, especially when building models with its visual designer, is straightforward
and often much quicker than setting up a LINQ to Entities environment. Microsoft’s official
encouragement to use the Entity Framework option may help guide your decision, but for
applications that have simple needs and access to SQL Server data, LINQ to SQL may be the
best query platform.
Understanding the Components of LINQ to SQL
The focus of an Entity Framework model is the XML-based definition of the three different
layers: conceptual, storage, and mapping. While LINQ to SQL can use an XML definition as
the basis for a specific data implementation, the focus of each table definition is the entity
class, a standard .NET class decorated with attributes from the System.Data.Linq.Mapping
namespace.
C#

[Table(Name="UnitOfMeasure")]
public class UnitOfMeasure
{
// ----- As defined in the database:
// ID bigint
// ShortName varchar(15)
// FullName varchar(50)
[Column(IsPrimaryKey = true)] public long ID;
[Column] public string ShortName;
[Column] public string FullName;
}
Visual Basic
<Table(Name:="UnitOfMeasure")>
Public Class UnitOfMeasure
' ----- As defined in the database:
' ID bigint
' ShortName varchar(15)
' FullName varchar(50)
<Column(IsPrimaryKey:=True)> Public ID As Long
<Column> Public ShortName As String
<Column> Public FullName As String
End Class
Dwonloaded from: iDATA.ws
334
Microsoft ADO.NET 4 Step by Step
Attributes, such as the TableAttribute and ColumnAttribute shown in this code block, inform
LINQ to SQL how to map class members to tables and columns in the database. Additional
attributes identify storage-level data types, intertable relationships, stored procedure defini-
tions, and other key items that let the application communicate cleanly with the external data
source.

The System.Data.Linq.DataContext class binds these class definitions with actual data
and acts much like the ObjectContext class in the Entity Framework. Derived versions of
DataContext include instances of the decorated classes, forming a class-style representation
of the actual database.
C#
public class SalesOrderLink : DataContext
{
// ----- Constructor establishes database connection.
public SalesOrder(string connectionString):
base(connectionString) {}
// ----- Table definitions to link with database.
public Table<Customer> Customers;
public Table<OrderEntry> Orders;
public Table<UnitOfMeasure> UnitsOfMeasure;
}
Visual Basic
Public Class SalesOrderLink
Inherits DataContext
' ----- Constructor establishes database connection.
Public Sub New(ByVal connectionString As String)
MyBase.New(connectionString)
End Sub
' ----- Table definitions to link with database.
Public Customers As Table(Of Customer)
Public Orders As Table(Of OrderEntry)
Public UnitsOfMeasure As Table(Of UnitOfMeasure)
End Class
After you have a defined context, using it with LINQ is a simple matter of creating an in-
stance of the context and adding its exposed members to a query.
Dwonloaded from: iDATA.ws

Chapter 20 Using LINQ to SQL
335
C#
using (SalesOrderLink context = new SalesOrderLink(connectionString))
{
var results = from cu in context.Customers
orderby cu.FullName
select new { CustomerID = cu.ID, CustomerName = cu.FullName };
}
Visual Basic
Using context As New SalesOrderLink(connectionString)
Dim results = From cu In context.Customers
Order By cu.FullName
Select CustomerID = cu.ID, CustomerName = cu.FullName
End Using
Except for the replacement of an ObjectContext by a DataContext, this query is identical to
the first LINQ to Entities query included in Chapter 19, “Using LINQ to Entities.”
Like its Entity Framework complement, LINQ to SQL uses the clauses in the query (in either
the standard LINQ form shown here or one built with extension methods) to craft SQL state-
ments that retrieve, project, filter, and sort the data returned by the query. Because of this,
you are limited in the types of non-LINQ-To-SQL data that you can include in the query. Only
data content and functionality that can be represented easily in a SQL statement are candi-
dates for inclusion in a LINQ to SQL query.
Despite this limitation, LINQ to SQL does a pretty good job at converting ordinary .NET ele-
ments into SQL equivalents. Comparisons with null (C#) and Nothing (Visual Basic) translate
into the expected IS NULL and IS NOT NULL forms. Using the Visual Basic Like pattern-
matching operator results in a similar LIKE comparison in the generated SQL. Including the
AddDays method on a date value within a LINQ to SQL query properly converts the expres-
sion into one that uses the related DATEADD function in T-SQL. For a complete list of all .NET
features that LINQ to SQL can use in database queries, see the “Data Types and Functions

(LINQ to SQL)” entry in the Visual Studio online help.
Using the Object Relational Designer
Although you can handcraft your own entity classes, a better option for large databases (and
even small ones) is to use the Object Relational (O/R) Designer, a drag-and-drop visual de-
signer that generates LINQ to SQL classes based on a graphical database model. To use the
O/R Designer, add a new “LINQ to SQL Classes” item to your Visual Basic or C# project.
Dwonloaded from: iDATA.ws

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×