如何:使用 LINQ 查询数据库 (Visual Basic)How to: Query a Database by Using LINQ (Visual Basic)

07/20/2015

本文内容

使用语言集成查询 (LINQ) 可以轻松地访问数据库信息和执行查询。Language-Integrated Query (LINQ) makes it easy to access database information and execute queries.

下面的示例演示如何创建一个新应用程序,用于对 SQL Server 数据库执行查询。The following example shows how to create a new application that performs queries against a SQL Server database.

本主题中的示例使用 Northwind 示例数据库。The examples in this topic use the Northwind sample database. 如果你的开发计算机上没有此数据库,可以从 Microsoft 下载中心进行下载。If you do not have this database on your development computer, you can download it from the Microsoft Download Center. 有关说明,请参阅 下载示例数据库。

备注

以下说明中的某些 Visual Studio 用户界面元素在计算机上出现的名称或位置可能会不同。Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. 这些元素取决于你所使用的 Visual Studio 版本和你所使用的设置。The Visual Studio edition that you have and the settings that you use determine these elements. 有关详细信息,请参阅个性化设置 IDE。For more information, see Personalizing the IDE.

创建与数据库的连接To create a connection to a database

在 Visual Studio 中, Server Explorer / Database Explorer单击Server Explorer / "视图" 菜单上的 "服务器资源管理器"数据库资源管理器打开服务器资源管理器数据库资源管理器。In Visual Studio, open Server Explorer/Database Explorer by clicking Server Explorer/Database Explorer on the View menu.

右键单击服务器资源管理器数据库资源管理器中的 "数据连接" / Database Explorer ,然后单击 "添加连接"。Right-click Data Connections in Server Explorer/Database Explorer and then click Add Connection.

指定与 Northwind 示例数据库的有效连接。Specify a valid connection to the Northwind sample database.

添加包含 LINQ to SQL 文件的项目To add a project that contains a LINQ to SQL file

在 Visual Studio 中的“文件”菜单上,指向“新建”,然后单击“项目”。In Visual Studio, on the File menu, point to New and then click Project. 选择 Visual Basic Windows 窗体应用程序 作为项目类型。Select Visual Basic Windows Forms Application as the project type.

在 “项目” 菜单上,单击 “添加新项”。On the Project menu, click Add New Item. 选择 " LINQ to SQL 类 " 项模板。Select the LINQ to SQL Classes item template.

命名文件 northwind.dbml。Name the file northwind.dbml. 单击“添加” 。Click Add. 将为 northwind 文件打开对象关系设计器 (O/R 设计器) 。The Object Relational Designer (O/R Designer) is opened for the northwind.dbml file.

添加要查询到 O/R 设计器的表To add tables to query to the O/R Designer

在服务器资源管理器 / 数据库资源管理器中,展开与 Northwind 数据库的连接。In Server Explorer/Database Explorer, expand the connection to the Northwind database. 展开 “表” 文件夹。Expand the Tables folder.

如果关闭了 O/R 设计器,则可以通过双击先前添加的 northwind 文件来重新打开它。If you have closed the O/R Designer, you can reopen it by double-clicking the northwind.dbml file that you added earlier.

单击 "Customers" 表,并将其拖到设计器的左窗格中。Click the Customers table and drag it to the left pane of the designer. 单击 "Orders" 表,并将其拖到设计器的左窗格中。Click the Orders table and drag it to the left pane of the designer.

设计器将 Customer Order 为您的项目创建新的和对象。The designer creates new Customer and Order objects for your project. 请注意,设计器将自动检测表之间的关系,并创建相关对象的子属性。Notice that the designer automatically detects relationships between the tables and creates child properties for related objects. 例如,IntelliSense 将显示 Customer 对象具有 Orders 与该客户相关的所有订单的属性。For example, IntelliSense will show that the Customer object has an Orders property for all orders related to that customer.

保存更改并关闭设计器。Save your changes and close the designer.

保存你的项目。Save your project.

添加代码以查询数据库并显示结果To add code to query the database and display the results

从 " 工具箱" 中,将 DataGridView 控件拖动到项目的默认 Windows 窗体 "Form1"。From the Toolbox, drag a DataGridView control onto the default Windows Form for your project, Form1.

双击 "Form1",将代码添加到 Load 窗体的事件中。Double-click Form1 to add code to the Load event of the form.

在将表添加到 O/R 设计器后,设计器将 DataContext 为您的项目添加一个对象。When you added tables to the O/R Designer, the designer added a DataContext object for your project. 除每个表的单个对象和集合外,此对象还包含访问这些表所需的代码。This object contains the code that you must have to access those tables, in addition to individual objects and collections for each table. DataContext项目的对象根据 .dbml 文件的名称进行命名。The DataContext object for your project is named based on the name of your .dbml file. 对于此项目,该 DataContext 对象的名称为 northwindDataContext 。For this project, the DataContext object is named northwindDataContext.

您可以在代码中创建的一个实例 DataContext ,并查询由 O/R 设计器指定的表。You can create an instance of the DataContext in your code and query the tables specified by the O/R Designer.

将以下代码添加到 Load 事件,以查询作为数据上下文的属性公开的表。Add the following code to the Load event to query the tables that are exposed as properties of your data context.

Dim db As New northwindDataContext

Dim londonCusts = From cust In db.Customers

Where cust.City = "London"

Select cust

DataGridView1.DataSource = londonCusts

按 F5 运行项目并查看结果。Press F5 to run your project and view the results.

下面是一些可以尝试执行的其他查询:Following are some additional queries that you can try:

Dim londonCustOrders = From cust In db.Customers,

ord In cust.Orders

Where cust.City = "London"

Order By ord.OrderID

Select cust.City, ord.OrderID, ord.OrderDate

DataGridView1.DataSource = londonCustOrdersDim custs = From cust In db.Customers

Where cust.Country = "France" And

(cust.CompanyName.StartsWith("F") Or

cust.CompanyName.StartsWith("V"))

Order By cust.CompanyName

Select cust.CompanyName, cust.City

DataGridView1.DataSource = custs

请参阅See also

linq 连接mysql_如何:使用 LINQ 查询数据库 - Visual Basic | Microsoft Docs相关推荐

  1. 熊猫图表 连接mysql_使用PHP将MySQL数据库连接到JavaScript图表

    熊猫图表 连接mysql When using a charting library like AnyChart, which makes visualizing data so quick and ...

  2. linq 连接mysql_使用LINQ访问数据库

    1. LINQ to SQL 概览 • 把.NET 类和 SQL 数据通过关系进行映射 • 把 LINQ 查询转化为 SQL 语言进行执行 • 支持对插入,更新,删除操作进行跟踪. 支持实体级别的验证 ...

  3. jedis连接mysql_使用Jedis操作Redis数据库

    Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如java.C.C#.C++.php.Node.js.Go等. 在官方网站里列一些Java的客户端,有Jedis.Redisson ...

  4. mbc连接mysql_研究miRNA,这些数据库你必须得知道!

    作者:WEE miRNA是近年来在多种真核细胞及病毒中发现的一类来源内源性染色体上的非编码单链RNA,长度为21-25nt的短序列,在进化上具有高度的保守性,能够通过与靶mRNA特异性的碱基互补配对, ...

  5. 达梦数据库如何连接MySQL_如何创建达梦数据库

    配置环境变量 Openfile 临时修改:ulimit -n 65536 /etc/security/limits.conf dmdba soft nofile 4096 dmdba hard nof ...

  6. wps vba连接mysql_第12篇 WPS数据库编程.pdf

    软件编程体系简介 � 目前在应用开发领域中, 主要分成两大编程体系,一种是 基于浏览器的B/S(Brower/Server)结构,另一种是C/S( Client/Server)结构. 第 章 数据库编 ...

  7. 新网的虚拟主机连接mysql_虚拟主机怎么导入数据库

    对于一些刚接触虚拟主机和数据库的新手站长来说对于数据库的导出和导入会不知所措,今天小编就简单说一下虚拟主机怎么导入数据库. 数据库是为了方便存储和管理数据而出现,它以一种特定的规律将数据储存在磁盘上. ...

  8. 普元连接mysql_普元平台初始化数据库

    数据库初始化完成,有错误发生: Failto run sql,due to ORA-00942: 表或视图不存在 SQL is:DROP TABLE EOSEJBRegister cascadecon ...

  9. mvc移动创建oracle表,使用 ASP.NET MVC (C#)在15分钟内创建电影数据库应用程序 | Microsoft Docs...

    使用 ASP.NET MVC 在 15 分钟内创建电影数据库应用程序 (C#)Create a Movie Database Application in 15 Minutes with ASP.NE ...

  10. mysql表收缩时从库也收缩么_收缩数据库 - SQL Server | Microsoft Docs

    收缩数据库Shrink a Database 03/14/2017 本文内容 适用于:Applies to: SQL ServerSQL Server(所有支持的版本)SQL ServerSQL Se ...

最新文章

  1. DEDE列表缩图中 给缩图添加alt锚文本信息的方法
  2. Java 理论与实践: JDK 5.0 中更灵活、更具可伸缩性的锁定机制--转载
  3. Django 3.2.5博客开发教程:一些常用的模板使用方法
  4. python打包时出现 Permission denied,然后dist下没有exe文件
  5. 02-线性结构2 一元多项式的乘法与加法运算 (20 分
  6. 学习SharePoint记录,微软给出的的Application Templates很有帮助呢
  7. android订阅管理,RXJAVA取消订阅封装-kotlin-Android
  8. kali2020进入单模式_出租车使用“滴滴”平台 司机希望恢复抢单模式 滴滴出行有回应...
  9. 此 Windows 副本不是正版的解决方案
  10. ADBAppium常见问题梳理
  11. 最新县及县以上行政区划代码(截止2014年10月31日)
  12. 微信支付-企业付款到零钱
  13. Flash cs3简单的动画制作
  14. 测试知识 - 关于电脑
  15. 视频教程-沐风老师3DMAX基础入门-3Dmax
  16. 计算机基础(9)——win10(5)——WIN10怎么把IE浏览器放到桌面
  17. Taro使用Vue3踏坑日记:Module not found: Can‘t resolve ‘@vue/server-renderer‘ in ‘/Users/...‘
  18. pytesseract 测试获取查策网编码字体
  19. Python 分数阶傅里叶变换
  20. linux需要看门狗喂狗程序,多任务看门狗, 喂狗方法

热门文章

  1. jpa删除数据后数据库无修改_jpa删除数据库
  2. Pannellum:实例之全景图自动旋转
  3. html怎样在雪景里插入文字,下雪朋友圈怎么配文字 雪景发朋友圈配一句话
  4. cast函数 oracle 日期_从 Oracle 到 PostgreSQL ,某保险公司迁移实践
  5. java实现多线程断点续传,上传下载
  6. SLAM基础_从零开始一起学习SLAM | 为啥需要李群与李代数?
  7. ubuntu16.04 安装完显卡驱动后分辨率固定640x480 解决
  8. 172篇文献:NUS颜水成等发布首篇《深度长尾学习》综述
  9. LeetCode之 x 的平方根
  10. jQuery AJAX 方法