本演练提供基本的端到端 LINQ to SQL 方案,用于在数据库中添加、修改和删除数据。 你将使用示例 Northwind 数据库的副本添加客户、更改客户的名称并删除订单。
注释
计算机可能会在以下说明中显示某些 Visual Studio 用户界面元素的不同名称或位置。 你拥有的 Visual Studio 版本以及所使用的设置决定了这些元素。 有关更多信息,请参阅 自定义 IDE。
本演练是使用 Visual C# 开发设置编写的。
先决条件
本演练需要如下内容:
- 本演练使用专用文件夹(“c:\linqtest6”)保存文件。 在开始演练之前创建此文件夹。 
- Northwind 示例数据库。 - 如果开发计算机上没有此数据库,可以从Microsoft下载站点下载它。 有关说明,请参阅 下载示例数据库。 下载数据库后,将northwnd.mdf文件复制到 c:\linqtest6 文件夹。 
- 从 Northwind 数据库生成的 C# 代码文件。 - 可以使用对象关系设计器或 SQLMetal 工具生成此文件。 本演练是使用 SQLMetal 工具并通过以下命令行编写的: - sqlmetal /code:“c:\linqtest6\northwind.cs” /language:csharp “C:\linqtest6\northwnd.mdf” /pluralize - 有关详细信息,请参阅 SqlMetal.exe(代码生成工具)。 
概述
本演练由六个主要任务组成:
- 在 Visual Studio 中创建 LINQ to SQL 解决方案。 
- 将数据库代码文件添加到项目。 
- 创建新的客户对象。 
- 修改客户的联系人姓名。 
- 删除订单。 
- 将这些更改提交到 Northwind 数据库。 
创建 LINQ to SQL 解决方案
在此第一个任务中,你将创建一个 Visual Studio 解决方案,其中包含生成和运行 LINQ to SQL 项目所需的引用。
创建 LINQ to SQL 解决方案
- 在 Visual Studio 文件 菜单上,指向 “新建”,然后单击“ 项目”。 
- 在“新建项目”对话框中的“项目类型”窗格中,单击“Visual C#”。 
- 在“ 模板 ”窗格中,单击“ 控制台应用程序”。 
- 在 “名称 ”框中,键入 LinqDataManipulationApp。 
- 在 “位置” 框中,验证要存储项目文件的位置。 
- 单击 “确定” 。 
添加 LINQ 引用和指令
本演练用到默认情况下您的项目中可能未安装的程序集。 如果 System.Data.Linq 未列为项目中的引用,请添加它,如以下步骤中所述:
添加 System.Data.Linq
- 在解决方案资源管理器中,右键点击引用,然后点击添加引用。 
- 在“ 添加引用 ”对话框中,单击 .NET,单击 System.Data.Linq 程序集,然后单击“ 确定”。 - 此程序集即被添加到项目中。 
- 在Program.cs顶部添加以下指令: - using System.Data.Linq; using System.Data.Linq.Mapping;
将 Northwind 代码文件添加到项目
这些步骤假定你已使用 SQLMetal 工具从 Northwind 示例数据库生成代码文件。 有关详细信息,请参阅本演练前面的先决条件部分。
将 “northwind” 代码文件添加到项目中
- 在 项目 菜单上,单击 添加现有项。 
- 在 “添加现有项 ”对话框中,导航到 c:\linqtest6\northwind.cs,然后单击“ 添加”。 - northwind.cs文件将添加到项目中。 
设置数据库连接
首先,测试与数据库的连接。 请注意,数据库 Northwnd 没有 i 字符。 如果在后续步骤中出现错误,请查看 northwind.cs 文件以确定 Northwind 部分类的拼写方式。
设置和测试数据库连接
- 在 Program 类的方法中键入或粘贴以下代码 - Main:- // Use the following connection string. Northwnd db = new Northwnd(@"c:\linqtest6\northwnd.mdf"); // Keep the console window open after activity stops. Console.ReadLine();
- 此时按 F5 测试应用程序。 - 此时会打开 控制台 窗口。 - 可以通过在控制台窗口中按 Enter 或单击 Visual Studio 调试菜单上的“停止调试”来关闭应用程序。 
创建新实体
创建新实体非常简单。 可以使用关键字创建对象(例如Customernew)。
在此部分和以下部分中,仅对本地缓存进行更改。 如果您不调用 SubmitChanges,则不会向数据库发送任何更改,一直到本演练结束都是如此。
添加新的 Customer 实体对象
- 在 - Customer方法中的- Console.ReadLine();之前添加以下代码以创建新的- Main。- // Create the new Customer object. Customer newCust = new Customer(); newCust.CompanyName = "AdventureWorks Cafe"; newCust.CustomerID = "ADVCA"; // Add the customer to the Customers table. db.Customers.InsertOnSubmit(newCust); Console.WriteLine("\nCustomers matching CA before insert"); foreach (var c in db.Customers.Where(cust => cust.CustomerID.Contains("CA"))) { Console.WriteLine("{0}, {1}, {2}", c.CustomerID, c.CompanyName, c.Orders.Count); }
- 按 F5 调试解决方案。 
- 在 控制台 窗口中按 Enter 停止调试并继续演练。 
更新实体
在以下步骤中,你将检索对象 Customer 并修改其属性之一。
更改客户名称
- 在 - Console.ReadLine();上方添加以下代码:- // Query for specific customer. // First() returns one object rather than a collection. var existingCust = (from c in db.Customers where c.CustomerID == "ALFKI" select c) .First(); // Change the contact name of the customer. existingCust.ContactName = "New Contact";
删除实体
使用相同的客户对象,可以删除第一个订单。
以下代码演示如何对行之间的关系进行分化,以及如何从数据库中删除行。 在 Console.ReadLine 之前添加以下代码,以查看如何删除对象:
删除行
- 将以下代码添加到 - Console.ReadLine();之前:- // Access the first element in the Orders collection. Order ord0 = existingCust.Orders[0]; // Access the first element in the OrderDetails collection. OrderDetail detail0 = ord0.OrderDetails[0]; // Display the order to be deleted. Console.WriteLine ("The Order Detail to be deleted is: OrderID = {0}, ProductID = {1}", detail0.OrderID, detail0.ProductID); // Mark the Order Detail row for deletion from the database. db.OrderDetails.DeleteOnSubmit(detail0);
将更改提交到数据库
创建、更新和删除对象所需的最后一步是实际将更改提交到数据库。 如果不执行此步骤,所做的更改只是本地更改,不会显示在查询结果中。
将更改提交到数据库
- 请将以下代码紧接在 - Console.ReadLine上方插入:- db.SubmitChanges();
- 插入以下代码(after - SubmitChanges) 以显示提交更改的前后效果:- Console.WriteLine("\nCustomers matching CA after update"); foreach (var c in db.Customers.Where(cust => cust.CustomerID.Contains("CA"))) { Console.WriteLine("{0}, {1}, {2}", c.CustomerID, c.CompanyName, c.Orders.Count); }
- 按 F5 调试解决方案。 
- 在 控制台 窗口中按 Enter 关闭应用程序。 
注释
通过提交更改添加新客户后,无法按原样再次执行此解决方案。 若要再次执行解决方案,请更改要添加的客户名称和客户 ID。