How to: Modify Data in a Database by Using LINQ - Visual Basic (original) (raw)
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.
To create a connection to a database
- 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.
To add a Project with a LINQ to SQL file
- 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 thenorthwind.dbmlfile.
To add tables to query and modify to the designer
- 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 thenorthwind.dbmlfile 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.
To add code to modify the database and display the results
- 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
Buttoncontrol. In the Properties window, set theNameof theButtoncontrol toAddButtonand theTexttoAdd. Select the second button and set theNameproperty toUpdateButtonand theTextproperty toUpdate. Select the third button and set theNameproperty toDeleteButtonand theTextproperty toDelete. - Double-click the Add button to add code to its
Clickevent. 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
Clickevent. 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
Clickevent. 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.