tailieunhanh - Visual studio 2010 part 23
Xử lý dữ liệu với LINQ to SQL LINQ to SQL cung cấp dịch vụ cho phép bạn giao tiếp với cơ sở dữ liệu SQL Server. Có rất nhiều loại khác của các nhà cung cấp, chẳng hạn như LINQ to Các đối tượng cho cơ sở dữ liệu chung (bao gồm SQL Server), LINQ to XML cho các nguồn dữ liệu XML, và LINQ to Oracle cho cơ sở dữ liệu Oracle | 200 Microsoft Visual Studio 2010 A Beginner s Guide Handling Data with LINQ to SQL The LINQ to SQL provider allows you to communicate with SQL Server databases. There are many other types of providers such as LINQ to Entities for generic databases which includes SQL Server LINQ to XML for XML data sources and LINQ to Oracle for Oracle databases. The preceding section showed you how to use the in-memory provider LINQ to Objects. However LINQ to SQL is the easiest database provider to learn and ships with VS. Once you learn LINQ to SQL the journey to other providers is easier. The following sections will show you how to set up LINQ to SQL perform queries and modify data. Setting Up LINQ to SQL Setting up LINQ to SQL involves running the LINQ to SQL Wizard and adding classes and methods. Behind the scenes LINQ to SQL generates code saving you a lot of work. The result of setting up LINQ to SQL is that you will have a data model which is an environment with classes that you can use to query and modify database data and call methods for invoking stored procedures. Before setting up LINQ to SQL you ll need to create a project a Console project for the purposes of this chapter . See Chapter 5 if you need a refresher on how to set up a Console project. Select Add New Item select LINQ to SQL Classes name the file and click Add. This will show you the LINQ to SQL Designer with two surfaces for classes and methods. Figure 7-11 shows the LINQ to SQL Designer with a couple of classes and a method. Figure 7-11 The LINQ to SQL Designer Chapter 7 Working with Data 201 To add entities to the LINQ to SQL Designer open Server Explorer select a database and open the Tables folder. Then drag and drop the Customer and Order tables from Server Explorer to the left surface of the LINQ to SQL Designer. You can see the Customer and Order classes in Figure 7-11 along with properties corresponding to the fields of each table in the database. The line between Customer and Order is
đang nạp các trang xem trước