LINQ as Language Extensions

Similar documents
TRAINING GUIDE. Rebranding Lucity Web

ASP.NET Security. 7/26/2017 EC512 Prof. Skinner 1

CP3343 Computer Science Project (Year) Technical Report Document. Mr Stephen Garner

Implementing a chat button on TECHNICAL PAPER

ASP.NET Pearson Education, Inc. All rights reserved.

Web Programming Paper Solution (Chapter wise)

Web Forms for Marketers 8.0 Rev: September 13, Web Forms for Marketers 8.0

Generation of a simple web-application in the Microsoft Visual Studio 2008 with the use of Silverlight Viewer for Reporting Services 2008

CHAPTER 1: GETTING STARTED WITH ASP.NET 4 1

Using an ArcGIS Server.Net version 10

70-562CSHARP. TS:MS.NET Framework 3.5, ASP.NET Application Development. Exam.

ASP.NET - MANAGING STATE

TS: Microsoft.NET Framework 3.5, ASP.NET Application Development

What is XHTML? XHTML is the language used to create and organize a web page:

Chapter 10. Database Applications The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Programming with the ASPxGridView

Create a cool image gallery using CSS visibility and positioning property

Chapter 9. Web Applications The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Postback. ASP.NET Event Model 55

Dreamweaver: Portfolio Site

Wicket and Aida/Web Brief Comparison

Course ID: 2310C Course Name: Developing Web Applications Using Microsoft Visual Studio 2008

EPiServer Programmer's Reference

SelectSurvey.NET Developers Manual

Please post comments or corrections to the Author Online forum at

Course Outline: Course 10267A: Introduction to Web Development with Microsoft Visual Studio 2010 Learning Method: Instructor-led Classroom Learning

HTML Overview. With an emphasis on XHTML

Google Chrome. Google Chrome FAQs

COURSE OUTLINE: OD10267A Introduction to Web Development with Microsoft Visual Studio 2010

Introduction to HTML5

Dreamweaver CS3 Lab 2

Introduction to Web Development with Microsoft Visual Studio 2010 (10267A)

MVC :: Understanding Models, Views, and Controllers

Developing Web Applications Using Microsoft Visual Studio 2008

2310C VB - Developing Web Applications Using Microsoft Visual Studio 2008 Course Number: 2310C Course Length: 5 Days

10265: Developing Data Access Solutions with Microsoft Visual Studio 2010 Duration: 5 Days Method: Instructor-Led

Microsoft ASP.NET Using Visual Basic 2008: Volume 1 Table of Contents

Webnodes Developers Quick Guide

Audience: Experienced application developers or architects responsible for Web applications in a Microsoft environment.

A designers guide to creating & editing templates in EzPz

COPYRIGHTED MATERIAL WHAT YOU WILL LEARN IN THIS CHAPTER:

ASP.NET Web Forms Programming Using Visual Basic.NET

INTRANET. EXTRANET. PORTAL.

GIMP WEB 2.0 MENUS. Before we begin this tutorial let s visually compare a standard navigation bar and a web 2.0 navigation bar.

Insert Data into Table using C# Code

Webnodes Developers Manual

Syncfusion Report Platform. Version - v Release Date - March 22, 2017

Quick Start Guide. This guide will help you get started with Kentico CMS for ASP.NET. It answers these questions:

Working with Data in ASP.NET 2.0 :: Using Existing Stored Procedures for the Typed DataSet s TableAdapters Introduction

Solution Explorer Copy Items in a Solution Files That Manage Solutions and Projects Solutions (SQL Server Management Studio) Associate a Query with a

Introduction to Web Development with Microsoft Visual Studio 2010

Bruce Moore Fall 99 Internship September 23, 1999 Supervised by Dr. John P.

Week Date Teaching Attended 8 1/3/2010 Lab 6: Secure Connections/ Toolkit 6

Join Queries in Cognos Analytics Reporting

Lecture 2: Tools & Concepts

Activating AspxCodeGen 4.0

Basics of Page Format

Using the JSON Iterator

"Charting the Course... MOC A Introduction to Web Development with Microsoft Visual Studio Course Summary

MVC :: Understanding Views, View Data, and HTML Helpers

10267A CS: Developing Web Applications Using Microsoft Visual Studio 2010

A Guide to CMS Functions

Foreign-Key Associations

Dreamweaver MX The Basics

CHAPTER 2 MARKUP LANGUAGES: XHTML 1.0

3-tier Architecture Step by step Exercises Hans-Petter Halvorsen

ASP.NET - MULTI THREADING

CSC 330 Object-Oriented

EXAM Web Development Fundamentals. Buy Full Product.

KillTest. 半年免费更新服务

Getting Started with ASP.NET 3.5

2.1 Read and Write XML Data. 2.2 Distinguish Between DataSet and DataReader Objects. 2.3 Call a Service from a Web Page

Working with Data in ASP.NET 2.0 :: Displaying Binary Data in the Data Web Controls Introduction

SYSTEMS DESIGN / CAPSTONE PROJECT MIS 413

The LINQ between XML and Database

Microsoft Expression Web Quickstart Guide

Metastorm BPM Release 7.6

Nebraska - eforms. Tips and Tricks

Unit Notes. ICAWEB411A Produce basic client-side script for dynamic web pages Topic 1 Introduction to JavaScript

Quick Start Guide. CodeGenerator v1.5.0

DE Introduction to Web Development with Microsoft Visual Studio 2010

Reading: Managing Files in Windows XP

Recommended Maintenance Plan for Siriusware Clients for SQL server 2005

10267 Introduction to Web Development with Microsoft Visual Studio 2010

What is ASP.NET? ASP.NET 2.0

Certified ASP.NET Programmer VS-1025

ASP.NET Using C# (VS2013)

Stephen Walther Senior Program Manager Microsoft Corporation

Final Web Application Create a new web site under c:\temp\webapps\ and name it Final. Create the following additional folders:

If this is the first time you have run SSMS, I recommend setting up the startup options so that the environment is set up the way you want it.

INTRODUCTION TO VISUAL BASIC 2010

CSC309 Midterm Exam Summer 2007

Courtesy of Clayton Fyfe. Lab 2 Runthrough

Websites WHAT YOU WILL LEARN IN THIS CHAPTER: WROX.COM CODE DOWNLOADS FOR THIS CHAPTER

The true beginning of our end. William Shakespeare, A Midsummer Night s Dream

Microsoft Web Development Fundamentals. Download Full Version :

Lab 4 CSS CISC1600, Spring 2012

Blu Ray Burning in MZ280 Step 1 - Set Toast up to burn a Blu-ray Video Disc.

OnBase Guide Creating Document Packet Template

LINQ to SAP. Jürgen Bäurle. July Parago Media GmbH & Co. KG

Transcription:

(Language Integrated Query) The main Topics in this lecture are: What is LINQ? Main Advantages of LINQ. Working with LINQ in ASP.Net Introduction: Suppose you are writing an application using.net. Chances are high that at some point you ll need to persist objects to a database, query the database and load the results back into objects. The problem is that in most cases, at least with relational databases, there is a gap between your programming language and the database. Good attempts have been made to provide object-oriented databases, which would be closer to object-oriented platforms and imperative programming languages like C# and VB.NET. However, after all these years, relational databases are still pervasive and you still have to struggle with data-access and persistence in all of your programs. The original motivation behind LINQ was to address the impedance mismatch between programming languages and databases. With LINQ, Microsoft s intention was to provide a solution for the problem of object-relational mapping, as well as simplify the interaction between objects and data sources. LINQ eventually evolved into a general-purpose languageintegrated querying toolset. This toolset can be used to access data coming from inmemory objects (LINQ to Objects), databases (LINQ to SQL), XML documents (LINQ to XML), a file-system, or from any other source. Advantages of LINQ The big question at this point is: why would we like, in the first place, to have a tool that makes working with programming languages, relational data and XML at the same time more convenient? At the origin of the LINQ project is a simple fact: How many applications access data or talk to a SQL database? The answer: the vast majority of them! Most applications deal with relational databases. Consequently, in order to program applications, learning a language like C# is not enough. You also have to learn SQL and the APIs that tie together 1

C# and SQL to form your full application. We ll start by taking a look at a piece of dataaccess code that uses the standard.net APIs. This will allow us to point out the common problems that are encountered in this kind of code. We will then extend our analysis to a higher level by showing how these problems exist with other kinds of data such as XML. You ll see that LINQ addresses a general impedance mismatch between data sources and programming languages. Finally a piece of short code sample will give you a glimpse at how LINQ is a solution to the problem. LINQ as Language Extensions LINQ allows you to access databases, XML documents and many other data sources by writing queries against these data sources. Rather than being simply syntactic sugar2 that would allow you to easily include SQL queries right into your C# code, LINQ provides you with the same expressive capabilities SQL offers but for your programming language. This is great because a declarative approach like the one LINQ offers allows writing code that is shorter and to the point. Getting Started with LINQ in Visual Studio Environment This next set of steps to get you started requires Visual Studio 2008. If you are using one of the Express versions of Visual Studio, be sure that any add-ons for LINQ are in place. Also, you will need to: 1. Open a new Web Site, and name it ShowLinq. 2. Open Solution Explorer and rename Default.aspx to ShowLinq.aspx. 3. Rename _Default to DoLinq in the C# script, and have it rename the ASP.NET inherit (inherits DoLinq). 4. Right-click the App_Data folder and select Add New Item. 5. Under Templates select SQL Server Database, and in the Name window change the default name to ServiceReg.mdf as shown in Figure 1. Click the Add button when finished. 6. Open the Server Explorer and you should see ServiceReg.mdf among the databases. 2

Right-click the Tables folder icon, and select Add New Table as shown in Figure 2. 7. When the table window opens, add the information shown in Figure 3. RegID is a primary key field and an identity field, so values enter automatically. 8. Save the table using the name RegClients. You will see the table listed in the Tables folder of the ServiceReg.mdf database in the Server Explorer. Figure 1 Selecting SQL database Figure 2: Setting the fields in a new table 3

Figure 3: Setting the fields in a new table 9. Right-click the RegClients table and select Show Table Data. The first time takes a little while. You will see the five fields you created. Click in the first field and press TAB to move to the LastName field. Begin entering records in this second field because the first field automatically enters values. Add about five records to have something to work with. Figure 4 shows an example. (Whenever you make changes in a record, the ReqID changes, so some of the ReqID values are out of sequence in Figure 4.) 10. In the Solution Explorer right-click the site (C:\..ShowLinq\), select Add ASP.NET Folder, and select App_Code. You will now see a new App_Code folder in the Solution Explorer. 11. Right-click the App_Code folder and select Add New Item. Select LINQ To SQL Classes, and in the Name window change the default name to RegClients.dbml and click Add. Figure 5 shows the correct settings Figure 4 Sample records 4

Figure 5 Adding the LINQ to SQL Classes template 12. After you click Add, a double window opens. This is the Object Relational Designer. Drag the RegClients table from the Server Explorer into the right window as shown in Figure 6. 13. From the Solution Explorer open the ShowLinq.aspx file. Then from the Toolbox Data menu drag a LinqDataSource control and drop it inside the <div> container. Switch to the Split mode or to Design mode. Figure 6 Dragging the table into the Object Relational Designer 5

14. Click the LinqDataSource object and open the Properties window. Next to ContextTypeName, select RegClientsDataContext as shown in Figure 7. 15. Set the TableName property to RegClients and the AutoPage property to True. Figure 7 Setting the ContextTypeName property 16. Set the following LinqDataSource1 properties to True: EnableDelete EnableInsert EnableUpdate 17. From the Toolbox Data menu, drag a DetailsView control beneath the LinqDataSource object as shown in Figure 8. 6

Figure 8 Adding the DetailsView control 18. Select the DetailsView control and in the Properties window select the LinqDataSource1 as the DataSourceID as shown in Figure 9. 19. Widen the DetailsView control, and then in the Properties window set the following to True: AutoPage AutoGenerateDeleteButton AutoGenerateEditButton AutoGenerateInsertButton 20. Once you have the settings complete, press CTRL-F5 to test the application. Figure 10 shows what you should see. Figure 9 Setting the DetailsView control DataSourceID property to LinqDataSource1 7

Figure 10 A LINQ-supported application Figure 11 Inserting a new record 21. To test the functionality of the application, click New and add a record as shown in Figure 11. Do not type anything into the RegID field because that is automatically filled. Once you ve finished, click Insert and your new record is added. All of those steps generated very little ASP.NET code. In looking at the ASP.NET portion of the application, all that you will see is shown in the following listing: ASP.NET ShowLinq.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowLinq.aspx.cs" Inherits="DoLinq" %> 8

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>asp.net Linq</title> </head> <body> <form id="form1" runat="server"> <div> <asp:linqdatasource ID="LinqDataSource1" runat="server" ContextTypeName="RegClientsDataContext" EnableDelete="True" EnableInsert="True" EnableUpdate="True" TableName="RegClients"> Figure 14-11 Inserting a new record Chapter 14: LINQ 365 </asp:linqdatasource> <asp:detailsview ID="DetailsView1" runat="server" DataSourceID="LinqDataSource1" AllowPaging="True" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateInsertButton="True" DataKeyNames="RegID" Height="50px" Width="205px"> </asp:detailsview> </div> </form> </body> 9