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) queries make it easy to access database information and modify values in the database.
The following example shows how to create a new application that retrieves and updates information in a SQL Server database.
The examples in this article use the Northwind sample database. To obtain the database, see Downloading Sample Databases.
In Visual Studio, open Server Explorer/Database Explorer by clicking the View menu, and then select Server Explorer/Database Explorer.
Right-click Data Connections in Server Explorer/Database Explorer, and 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.
The designer creates a new Customer object for your project.
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.
When you added tables to the O/R Designer, the designer added a DataContext object to your project. This object contains code that you can use to access the Customers table. It also contains code that defines a local Customer object and a Customers collection for the 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 object in your code and query and modify the Customers collection specified by the O/R Designer. Changes that you make to the Customers collection are not reflected in the database until you submit them by calling the SubmitChanges method of the DataContext object.
Double-click the Windows Form, Form1, to add code to the Load event to query the Customers table that is exposed as a property of your DataContext. Add the following code:
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
From the Toolbox, drag three Button controls onto the form. Select the first Button
control. In the Properties window, set the Name
of the Button
control to AddButton
and the Text
to Add
. Select the second button and set the Name
property to UpdateButton
and the Text
property to Update
. Select the third button and set the Name
property to DeleteButton
and the Text
property to Delete
.
Double-click the Add button to add code to its Click
event. Add the following code:
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
Double-click the Update button to add code to its Click
event. Add the following code:
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
Double-click the Delete button to add code to its Click
event. Add the following code:
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
Press F5 to run your project. Click Add to add a new record. Click Update to modify the new record. Click Delete to delete the new record.
.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
Documentation
How to: Query a Database by Using LINQ - Visual Basic
Learn more about: How to: Query a Database by Using LINQ (Visual Basic)
How to: Combine Data with LINQ by Using Joins - Visual Basic
Learn more about: How to: Combine Data with LINQ by Using Joins (Visual Basic)
How to: Return a LINQ Query Result as a Specific Type - Visual Basic
Learn more about: How to: Return a LINQ Query Result as a Specific Type (Visual Basic)