700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > linq 连接mysql_如何:使用 LINQ 查询数据库 - Visual Basic | Microsoft Docs

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

时间:2024-05-05 12:04:57

相关推荐

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

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

07/20/

本文内容

使用语言集成查询 (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

(panyName.StartsWith("F") Or

panyName.StartsWith("V"))

Order By panyName

Select panyName, cust.City

DataGridView1.DataSource = custs

请参阅See also

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。