Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
21Activity
0 of .
Results for:
No results containing your search query
P. 1
LINQ to SQL Overview CSharp and VB

LINQ to SQL Overview CSharp and VB

Ratings: (0)|Views: 1,679|Likes:
Published by Sreenivasarao

More info:

Published by: Sreenivasarao on Aug 27, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

11/04/2012

pdf

text

original

 
Chapter 0 LINQ to SQL
LINQ to SQL
.NET Language-Integrated Query for Relational Data
Dinesh Kulkarni, Luca Bolognese, Matt Warren, Anders Hejlsberg, Kit GeorgeMicrosoft CorporationFebruary 2007Applies to:Microsoft
®
Visual Studio
®
Code Name "Orcas"Microsoft
®
.Net Framework 3.5
Summary:
LINQ to SQL, a component of Visual Studio Code Name "Orcas", provides a run-timeinfrastructure for managing relational data as objects without giving up the ability to query. Itdoes this by translating language-integrated queries into SQL for execution by the database andthen translating the tabular results back into objects you define. Your application is then free tomanipulate the objects while LINQ to SQL stays in the background tracking your changesautomatically.
Table of Contents
LINQ to SQL......................................................................................................1
1. Introduction
Most programs written today manipulate data in one way or another and often this data is storedin a relational database. Yet there is a huge divide between modern programming languages anddatabases in how they represent and manipulate information. This impedance mismatch is visiblein multiple ways. Most notable is that programming languages access information in databasesthrough APIs that require queries to be specified as text strings. These queries are significantportions of the program logic yet they are opaque to the language, unable to benefit fromcompile-time verification and design-time features like IntelliSense
®
.Of course, the differences go far deeper than that. How information is represented, the datamodel, is quite different between the two. Modern programming languages define information inthe form of objects. Relational databases use rows. Objects have unique identity, as each instanceis physically different from another. Rows are identified by primary key values. Objects havereferences that identify and link instances together. Rows are left intentionally distinct requiringrelated rows to be tied together loosely using foreign keys. Objects stand alone, existing as longas they are still referenced by another object. Rows exist as elements of tables, vanishing as soonas they are removed.It is no wonder that applications expected to bridge this gap are difficult to build and maintain. Itwould certainly simplify the equation to get rid of one side or the other. Yet relational databasesprovide critical infrastructure for long-term storage and query processing, and modernprogramming languages are indispensable for agile development and rich computation.Until now, it has been the job of the application developer to resolve this mismatch in eachapplication separately. The best solutions so far have been elaborate database abstraction layers
 
Chapter 0 LINQ to SQL
that ferry the information between the application's domain specific object models and the tabularrepresentation of the database, reshaping and reformatting the data each way. Yet by obscuringthe true data source these solutions end up throwing away the most compelling feature of relational databases; the ability for the data to be queried.LINQ to SQL, a component of Visual Studio Code Name "Orcas", provides a run-timeinfrastructure for managing relational data as objects without giving up the ability to query. Itdoes this by translating language-integrated queries into SQL for execution by the database andthen translating the tabular results back into objects you define. Your application is then free tomanipulate the objects while LINQ to SQL stays in the background tracking your changesautomatically.
LINQ to SQL is designed to be non-intrusive to your application. It is possible to migratecurrent ADO.NET solutions to LINQ to SQL in a piecemeal fashion, sharing the sameconnections and transactions, since LINQ to SQL is simply another component in the ADO.NETfamily. LINQ to SQL also has extensive support for stored procedures, allowing reuse of theexisting enterprise assets.
LINQ to SQL applications are easy to get started. Objects linked to relational data can bedefined just like normal objects, only decorated with attributes to identify how propertiescorrespond to columns. Of course, it is not even necessary to do this by hand. A design-timetool is provided to automate translating pre-existing relational database schemas into objectdefinitions for you.Together, the LINQ to SQL run-time infrastructure and design-time tools significantly reduce theworkload for the database application developer. The following chapters provide an overview of how LINQ to SQL can be used to perform common database related tasks. It is assumed that thereader is familiar with Language-Integrated Query and the Standard Query Operators.LINQ to SQL is language-agnostic. Any language built to provide Language-Integrated Query canuse it to enable access to information stored in relational databases. The samples in thisdocument are shown in both C# and Visual Basic; LINQ to SQL can be used with the LINQ-enabled version of the Visual Basic compiler as well.
2. A Quick Tour
The first step in building a LINQ to SQL application is declaring the object classes you will use torepresent your application data. Let's walk through an example.
2.1 Creating Entity Classes
We will start with a simple class
Customer
and associate it with the customers table in theNorthwind sample database. To do this, we need only apply a custom attribute to the top of theclass declaration. LINQ to SQL defines the
Table
attribute for this purpose.
[C#]
[Table(Name="Customers")] public class Customer{public stringCustomerID; public stringCity;
 
Chapter 0 LINQ to SQL
}
[Visual Basic]
<Table(Name:="Customers")> _ Public Class CustomerPublicCustomerIDAs StringPublicCityAs StringEnd Class
The
Table
attribute has a
Name
property that you can use to specify the exact name of thedatabase table. If no
Name
property is supplied LINQ to SQL will assume the database table hasthe same name as the class. Only instances of classes declared as tables will be able to be storedin the database. Instances of these types of classes are known as
entities,
and the classesthemselves,
entity classes
.In addition to associating classes to tables you will need to denote each field or property youintend to associate with a database column. For this, LINQ to SQL defines the
Column
attribute.
[C#]
[Table(Name="Customers")] public class Customer{[Column(IsPrimaryKey=true)] public stringCustomerID; [Column]public stringCity;}
[Visual Basic]
<Table(Name:="Customers")> _ Public Class Customer<Column(IsPrimaryKey:=true)> _ PublicCustomerIDAs String<Column> _PublicCityAs StringEnd Class

Activity (21)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
mummyb liked this
mummyb liked this
morteza2328 liked this
Gevita Gv liked this
Jeeva liked this
kshor liked this
elephantaa liked this
anversadat21 liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->