You are on page 1of 4

Video How to: Simple Object Model and Query (Visual Basic

)
(LINQ to SQL)
Transcript

I'm Harry Miller, and I'm going to show you a basic LINQ to SQL example. I'll create an
entity class that models the Customers table in the sample Northwind database, and then
create a simple query to list customers who are located in London.

LINQ to SQL provides a run-time infrastructure for managing relational data as objects.
The data model of a relational database is mapped to an object model that is expressed in
Visual Basic. At run time, LINQ to SQL translates the Visual Basic into database queries,
and then translates the returned results into objects.

Creating the Project
Although typically you might use the Object Relational Designer to create entity classes
and relationships visually, for this example I'll create these classes manually, to help show
the LINQ to SQL concepts. I'll name it LinqConsoleApp.

I need to add two references, so in the Add Reference dialog box, on the .NET tab, I'll
select System.Data.Linq and click OK.

I also need to add System.Windows.Forms from the .NET tab because there's a
message box later in the example.

Adding the Code
In Module1, I'll put three Imports statements for LINQ and Windows Forms:

Imports System.Data.Linq

Imports System.Data.Linq.Mapping

Imports System.Windows.Forms

Now I'll create an entity class in the module. An entity class is a class that's mapped to a
database table. To do this mapping, all I need to do is apply the Table attribute. The

Name property specifies the name of the table in the database.

<Table(Name:="Customers")> _

Public Class Customer

which means that LINQ to SQL can store and retrieve values directly. Storage:="_CustomerID")> _ Public Property CustomerID() As String Get Return Me. which designates them as representing columns in the database table._City = value End Set End Property ._CustomerID = value End Set End Property Private _City As String <Column(Storage:="_City")> _ Public Property City() As String Get Return Me. Both are designated for private storage. There are two properties here: CustomerID and City. The CustomerID property is a primary key column in the database. instead of using public accessors that might include business logic._City End Get Set(ByVal value As String) Me. Private _CustomerID As String <Column(IsPrimaryKey:=True.End Class The next step is to designate properties to represent database columns. They both get the Column attribute._CustomerID End Get Set(ByVal value As String) Me.

City = "London" _ Select cust I'll also add a line of code here to show the SQL commands that LINQ to SQL generates. this code sets variables with formatting information such as the title and the basic appearance. title As String = "London customers:". Dim Customers As Table(Of Customer) = _ db. Dim db As New DataContext _ ("c:\linqtest\northwnd.Log = Console. I'll need to create a For Each loop. Dim msg As String = "".mdf") I'll also declare a Table(Of Customer) to act as the logical. db.Format(custObj. in case I need to do some debugging.GetTable(Of Customer)() Now to create a query to find out which customers in the Customers table are located in London. This shows the commands that are sent to the database. typed table for the queries I'll create against the Customers table. _ response As MsgBoxResult. The DataContext is the main channel for retrieving objects from the database and for submitting changes. For each object returned by the query. Dim custQuery = _ From cust In Customers _ Where cust.Out Now I'll put in the For Each loop to run the query. This code just describes the query—to actually run it.Information .I need to set up a connection to the Northwind database that I'm using. style As MsgBoxStyle = _ MsgBoxStyle. and format a message box to show the results. For Each custObj In custQuery msg &= String.CustomerID & vbCrLf) Next For the message. so I'll create a DataContext object. add it to the message.

the console window shows the generated SQL code. blogs. Just go to msdn. title) Running the Program Now I'll run it. response = MsgBox(msg. and videos at the Visual Basic Developer Center.com/vbasic. style. Also. For More Information You can get more information about developing Visual Basic applications in the Visual Basic Help. samples.This code shows the message using the variables. and I get my list of customers in London.microsoft. . You can find other resources such as technical articles.