Creating LINQ to SQL Entities in VB.NET


Server Intellect


Creating LINQ to SQL Entities in VB.NET

This tutorial was created with Visual Studio .NET 2008, but can be recreated in 2005, after downloading and installing Microsoft's LINQ Community Technology Preview release, which can be downloaded from here.

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

Visual Studio.NET 2008 makes it very easy for us to create LINQ to SQL Entities using the Object Relational Designer. What it does is creates classes and methods that relate to the database columns and tables. This makes it possible for us to communicate with the data using LINQ (Language Integrated Query).

This tutorial will show how we can bypass the Designer and write the class ourselves, so that we get a better understanding of what's going on. For this example, we will be using a SQL database with one table and three columns - id, name, and city.
Once we have our database set up, we will create a new class to represent the database table structure. It should look something like this:

Imports System
Imports System.Data.Linq.Mapping

<Table(name:="tblPeople")> _
Public Class people
Private _Id As Integer
Private _name As String
Private _city As String

<Column(IsPrimaryKey:=True, IsDbGenerated:=True)> _
Public Property Id() As Integer
Get
Return _Id
End Get

Set(ByVal value As Integer)
_Id = value
End Set
End Property

<Column(CanBeNull:=True)> _
Public Property name() As String
Get
Return _name
End Get

Set(ByVal value As String)
_name = value
End Set
End Property

<Column(CanBeNull:=True)> _
Public Property city() As String
Get
Return _city
End Get

Set(ByVal value As String)
_city = value
End Set
End Property
End Class

It is advised to always include the table name in the class, although it is not really required if the class is named the same as the table in the SQL database. You should always declare the Primary Key, especially if you are planning on making changes to the database. IsDbGenerated is also used where the database will auto-generate the values upon insert.
In the class, we need to define a [Column] for each in the database table, and then the name of the column should be represented by the public string (or int, etc.)

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

Next, we are going to display the data with a GridView, and we will also add a textbox and button to the page to allow searching of the database.
Our ASPX page will look something like this:

<form id="form1" runat="server">
Name: <asp:TextBox ID="txtName" runat="server" /><br />
<asp:Button ID="butSearch" runat="server" Text="Search"
onclick="butSearch_Click" /><br />
<br />
<asp:GridView ID="GridView1" runat="server" />
</form>

We are also going to need a connection string. We have the following in our Web.config file:

<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

Now we have our database set up, and our class representing the database, we can add to the code-behind to interact with our database. We will use the textbox to allow the user to search for matches in the name column of the database, and the code will go under the button click event:

Dim con As String = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim db As New DataContext(con)
Dim tPerson As Table(Of people)
tPerson = db.GetTable(Of people)()

GridView1.DataSource = tPerson.Where(Function(p) p.name.Contains(txtName.Text))
GridView1.DataBind()

The above code is using a Lambda Expression, which can be a lot shorter than using a regular LINQ Query. We are simply selecting the records that match our request, and then binding the gridview with the data returned from this query. Note that GetTable<> references the class name; not the table name (if different).
When we run this web application, we are greeted with just the textbox and the button. If we leave the textbox blank and hit the button, we should be shown all the records in the database. If we enter some text, we will be shown matches from the database.

The entire code-behind looks something like this:

Imports System.Web.Configuration
Imports System.Linq
Imports System.Data.Linq

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub butSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butSearch.Click
Dim con As String = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim db As New DataContext(con)
Dim tPerson As Table(Of people)
tPerson = db.GetTable(Of people)()

GridView1.DataSource = tPerson.Where(Function(p) p.name.Contains(txtName.Text))
GridView1.DataBind()
End Sub
End Class

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 


Comments
More Article said:

thank you

Posted 01/28/2010 at 1:55 AM
Online Kasino Check said:

TThank you for putting up this tutorial, because of it, my program is working here's no doubt that LINQ is one of the headline features of Visual Studio Orcas. I've been trying out some LINQ queries; it's a very interesting new area with massive potential. It seems though that most books and articles available at the moment are C# based..

Posted 02/27/2010 at 5:37 AM
Call Center Software said:

LINQ has many advantages over the conventional data querying techniques used in ASP .NET language integrated querying, receiving data from various data sources, compile time check of queries and its performance gain. This LINQ tutorial is about creating LINQ to SQL entities in VB .NET. In this article the author has also given the tip to use LINQ with Visual Studio .NET 2005 (LINQ is usually used with Visual Studio .NET 2008). At first section he shows how a new class is created to represent the database table structure. The next section is about displaying the data in GridView and some searching functions. The last section is about creating facility to enter search string and displaying the search result. This article is very useful for the beginners in this field to learn the LINQ basics as it is done in simple language and with the help of an example.

Posted 05/06/2010 at 12:04 PM
Girish Nama said:

"This tutorial will show how we can bypass the Designer and write the class ourselves, so that we get a better understanding of what's going on"

thnks for this. one must know what is going on inside. always VS creates all the codeing for us and we do NOT know about it.

Posted 05/22/2010 at 8:39 AM
Neelam Fancy Jewellery India said:

the Object Relational Designer. What it does is creates classes and methods that relate to the database columns and tables. This makes it possible for us to communicate with the data using LINQ (Language Integrated Query.

Posted 06/04/2010 at 2:00 AM
Neelam Fancy Jewellery India said:

the Object Relational Designer. What it does is creates classes and methods that relate to the database columns and tables. This makes it possible for us to communicate with the data using LINQ (Language Integrated Query.

Posted 06/04/2010 at 2:00 AM
online classifieds said:

The days of ADODB.RecordSet and the oft-forgotten MoveNext are gone, replaced by the powerful and flexible capabilities of Microsoft ADO.NET. Our new arsenal is the System.Data namespace, featuring lightning-fast DataReaders, feature-rich DataSets, and packaged in a capable object-oriented model.

Posted 06/08/2010 at 6:28 AM
Bedroom Furniture said:

The days of ADODB.RecordSet and the oft-forgotten MoveNext are gone, replaced by the powerful and flexible capabilities of Microsoft ADO.NET. Our new arsenal is the System.Data namespace, featuring lightning-fast DataReaders

Posted 06/11/2010 at 1:15 AM
Agile Informatics Nishu said:

This tutorial will show how we can bypass the Designer and write the class ourself, so that we get a better understanding of what's going on. For this example, we will be using a SQL database with one table and three columns - id, name, and city.

Posted 06/11/2010 at 1:32 AM
Agile Informatics Nishu said:

This is where the ADO.NET Entity Framework comes in; it allows you to deal with the (true) entities represented in the database in your application code by abstracting the groundwork and maintenance code work away from you. A very crude description of the ADO.NET Entity Framework would be "It allows you to deal with database concepts in your code."

Posted 06/11/2010 at 1:33 AM
kuber infotek gupta said:

The days of ADODB.RecordSet and the oft-forgotten MoveNext are gone, replaced by the powerful and flexible capabilities of Microsoft ADO.NET. Our new arsenal is the System.Data namespace, featuring lightning-fast DataReaders, feature-rich DataSets, and packaged in a capable object-oriented model

Posted 06/11/2010 at 5:19 AM
Women Health Care Center said:

The days of ADODB.RecordSet and the oft-forgotten MoveNext are gone, replaced by the powerful and flexible capabilities of Microsoft ADO.NET. Our new arsenal is the System.Data namespace, featuring lightning-fast DataReaders, feature-rich DataSets, and packaged in a capable object-oriented model. It's no surprise that we have such tools at our disposal. Any 3-tier architecture relies on a solid Data Access Layer (DAL) to elegantly connect the data layer to the business layer. A quality DAL helps promote code reuse, is key to good performance, and is totally transparent.

Posted 06/15/2010 at 6:34 AM
limo rental in los angeles said:

The days of ADODB.RecordSet and the oft-forgotten MoveNext are gone, replaced by the powerful and flexible capabilities of Microsoft ADO.NET. Our new arsenal is the System.Data namespace, featuring lightning-fast DataReaders, feature-rich DataSets, and packaged in a capable object-oriented model. It's no surprise that we have such tools at our disposal.

Posted 06/25/2010 at 7:03 AM
Luxury Villas said:

Well...If we leave the textbox blank and hit the button, we should be shown all the records in the database. If we enter some text, we will be shown matches from the database.Thanks and Regards

Posted 07/07/2010 at 10:14 AM
flex chat said:

How long have you been in this field? Certainly, you know a lot more than I do, I would love to know your sources!

Posted 07/22/2010 at 5:11 AM
DJ Equipment said:

Hmmmmm tutorial will show how we can bypass the Designer and write the class ourselves, so that we get a better understanding of what's going on. For this example, we will be using a SQL database with one table and three columns - id, name, and city.

Posted 07/31/2010 at 11:09 AM
Flyer Design said:

Very informative content. I’ve found your site via Google and I’m really glad about the information you provide in your posts. Keep up the good work!

Posted 07/31/2010 at 2:27 PM
Business Gifts said:

I am very interested in your article, I think your articles are so interesting that I need more information, go is berkaya and I will always support you. I say many thanks to you.

Posted 07/31/2010 at 2:28 PM
SEO Consultant said:

This will include the customer information for display. You have to use teh include format if you want to display information. If you only want to limit it to a customer, but not display customer info, you do not have to do the include.

Posted 07/31/2010 at 2:29 PM
Wedding Planner said:

This is where the ADO.NET Entity Framework comes in; it allows you to deal with the (true) entities represented in the database in your application code by abstracting the groundwork and maintenance code work away from you. A very crude description of the ADO.NET Entity Framework would be "It allows you to deal with database concepts in your code."

Posted 08/03/2010 at 4:27 AM
simulation assurance auto said:

Information like the one you mentioned here will be very useful to me!.I love books and used to visit bookshops regularly.I just visit your blog.The articles are quite good and I have already used to visit this site regularly.A nice one...

Posted 08/04/2010 at 3:53 AM
online casino said:

however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues

Posted 08/06/2010 at 11:25 PM
LED Pods said:

When we run this web application, we are greeted with just the textbox and the button. If we leave the textbox blank and hit the button, we should be shown all the records in the database. If we enter some text, we will be shown matches from the database.

Posted 08/08/2010 at 4:06 PM
search engine placement marketing said:

the database table structure. The next section is about displaying the data in GridView and some searching functions. The last section is about creating facility to enter search string and displaying the search result. This article is very useful for the beginners in this field to learn the LINQ basics as it is done in simple language and with the help

Posted 08/13/2010 at 1:40 PM
Phone Psychics said:

we will create a new class to represent the database table structure. It should look something like this:

Posted 08/13/2010 at 3:57 PM
Insurance said:

interact with our database. We will use the textbox to allow the user to search for matches in the name column of the database, and the code will go under the button click event:

Posted 08/16/2010 at 1:44 AM
online blackjack said:

This article gives the light in which we can observe the reality. this is very nice one and gives in depth information. thanks for this nice article Good post.....Valuable information for all.I will recommend my friends to read this for sure…

Posted 08/18/2010 at 8:36 AM
travel agents said:

Th4t be an epic da shizzi4 post, th4nkie 4it & in da futures we'll be seeing more of it

Posted 08/18/2010 at 5:22 PM
cruises said:

We7ll I8be dat9 ogr6e speekie da speekie, gratz & than4x

Posted 08/18/2010 at 5:24 PM
flight center said:

heb7e sh8at be th34nkie 4it on da posting left & righ8ty

Posted 08/18/2010 at 5:24 PM
maison de credit said:

Really impressed! Everything is very open and very clear explanation of issues. It contains truly information. Your website is very useful. Thanks for sharing. Looking forward to more!

Posted 08/25/2010 at 4:39 AM
robseller81 said:

Great post! It is very useful for me.

Posted 08/26/2010 at 5:26 AM
robseller81 said:

Great post! It is very useful for me.

Posted 08/26/2010 at 5:27 AM
Phoenix Pools said:

Your post is very helpful

Posted 08/27/2010 at 10:57 PM

Leave a Comment