Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Language-Integrated Query (LINQ) makes it easy to access database information and execute queries.
The following example shows how to create a new application that performs queries against a SQL Server database.
The examples in this article use the Northwind sample database. To obtain the database, see Downloading Sample Databases.
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalizing the IDE.
In Visual Studio, open Server Explorer/Database Explorer by clicking Server Explorer/Database Explorer on the View menu.
Right-click Data Connections in Server Explorer/Database Explorer and then click Add Connection.
Specify a valid connection to the Northwind sample database.
In Visual Studio, on the File menu, point to New and then click Project. Select Visual Basic Windows Forms Application as the project type.
On the Project menu, click Add New Item. Select the LINQ to SQL Classes item template.
Name the file northwind.dbml
. Click Add. The Object Relational Designer (O/R Designer) is opened for the northwind.dbml file.
In Server Explorer/Database Explorer, expand the connection to the Northwind database. Expand the Tables folder.
If you have closed the O/R Designer, you can reopen it by double-clicking the northwind.dbml file that you added earlier.
Click the Customers table and drag it to the left pane of the designer. Click the Orders table and drag it to the left pane of the designer.
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. 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.
From the Toolbox, drag a DataGridView control onto the default Windows Form for your project, Form1.
Double-click Form1 to add code to the Load
event of the form.
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. The DataContext object for your project is named based on the name of your .dbml file. For this project, the DataContext object is named northwindDataContext
.
You can create an instance of the DataContext in your code and query the tables specified by the O/R Designer.
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
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 = londonCustOrders
Dim 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
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Use databases in a .NET Aspire project - Training
Learn about the database systems that .NET Aspire can connect to using built-in integrations. Then see how to configure connections to, and store data in, relational and nonrelational databases.