语言集成查询 (LINQ) 查询轻松访问数据库的信息和修改数据库中的值。
下面的示例演示如何创建新的应用程序检索和更新信息的 SQL Server 数据库中。
本主题中的示例使用 Northwind 示例数据库。 如果在开发计算机上没有此数据库,您可以从 Microsoft 下载中心下载它。 有关说明,请参阅下载示例数据库。
若要创建与数据库的连接
-
在 Visual Studio 中打开服务器资源管理器/数据库资源管理器通过单击视图菜单,并选择服务器资源管理器/数据库资源管理器。
-
右键单击数据连接中服务器资源管理器/数据库资源管理器,然后单击添加连接。
-
指定到 Northwind 示例数据库的有效连接。
若要将使用 LINQ 项目添加到 SQL 文件
-
在 Visual Studio 中的“文件”菜单上,指向“新建”,然后单击“项目”。 选择 Visual Basic Windows 窗体应用程序作为项目类型。
-
在 “项目” 菜单上,单击 “添加新项”。 选择LINQ to SQL 类项模板。
-
为
northwind.dbml文件命名。 单击 添加。 对象关系设计器 (O/R 设计器) 打开供northwind.dbml文件。
若要添加表查询和修改到设计器
-
在中服务器资源管理器/数据库资源管理器,扩展连接到 Northwind 数据库。 展开表文件夹。
如果已关闭 O/R 设计器,你可以重新打开它通过双击
northwind.dbml前面添加的文件。 -
单击客户表并将其拖到设计器的左窗格中。
在设计器创建新的 Customer 对象为你的项目。
-
保存所做的更改并关闭设计器。
-
保存你的项目。
添加代码以修改数据库并显示结果
-
从工具箱,拖动xref:System.Windows.Forms.DataGridView控件拖到你的项目,Form1 的默认 Windows 窗体上。
-
当表添加到 O/R 设计器时,在设计器添加xref:System.Data.Linq.DataContext到你的项目的对象。 此对象包含可用于访问客户表的代码。 它还包含用于定义本地客户对象和表的客户集合的代码。 xref:System.Data.Linq.DataContext对象将项目命名为根据.dbml 文件的名称。 对于此项目,xref:System.Data.Linq.DataContext对象被命名为
northwindDataContext。可以创建的实例xref:System.Data.Linq.DataContext中您的代码和查询对象并修改指定由 O/R 设计器的客户集合。 之前通过调用将其提交到的客户集合所做的更改不会反映在数据库中xref:System.Data.Linq.DataContext.SubmitChanges*方法的xref:System.Data.Linq.DataContext对象。
双击 Windows 窗体,Form1,若要将代码添加到xref:System.Windows.Forms.Form.Load作为属性公开的 Customers 表的查询的事件在xref:System.Data.Linq.DataContext。 添加以下代码:
Private db As northwindDataContext Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles MyBase.Load db = New northwindDataContext() RefreshData() End Sub Private Sub RefreshData() Dim customers = From cust In db.Customers Where cust.City(0) = "W" Select cust DataGridView1.DataSource = customers End Sub -
从工具箱,将三个xref:System.Windows.Forms.Button拖到窗体控件。 选择第一个
Button控件。 在中属性窗口中,将Name的Button控制对AddButton并Text到Add。 选择第二个按钮,然后设置Name属性设置为UpdateButton并Text属性设置为Update。 选择第三个按钮,然后设置Name属性设置为DeleteButton并Text属性设置为Delete。 -
双击外按钮以将代码添加到其
Click事件。 添加以下代码:Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles AddButton.Click Dim cust As New Customer With { .City = "Wellington", .CompanyName = "Blue Yonder Airlines", .ContactName = "Jill Frank", .Country = "New Zealand", .CustomerID = "JILLF"} db.Customers.InsertOnSubmit(cust) Try db.SubmitChanges() Catch ' Handle exception. End Try RefreshData() End Sub -
双击更新按钮以将代码添加到其
Click事件。 添加以下代码:Private Sub UpdateButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs ) Handles UpdateButton.Click Dim updateCust = (From cust In db.Customers Where cust.CustomerID = "JILLF").ToList()(0) updateCust.ContactName = "Jill Shrader" updateCust.Country = "Wales" updateCust.CompanyName = "Red Yonder Airlines" updateCust.City = "Cardiff" Try db.SubmitChanges() Catch ' Handle exception. End Try RefreshData() End Sub -
双击删除按钮以将代码添加到其
Click事件。 添加以下代码:Private Sub DeleteButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs ) Handles DeleteButton.Click Dim deleteCust = (From cust In db.Customers Where cust.CustomerID = "JILLF").ToList()(0) db.Customers.DeleteOnSubmit(deleteCust) Try db.SubmitChanges() Catch ' Handle exception. End Try RefreshData() End Sub -
按 F5 运行项目。 单击添加添加新记录。 单击更新修改新记录。 单击删除删除新记录。
请参阅
历史资料。此文对应 2019-08-18 的中文文档快照,适用产品版本和命令行为以原文为准。
来源与署名:Microsoft 和文档贡献者。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体和导航标记。
