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.

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


_

Public Class people

Private _Id As Integer

Private _name As String

Private _city As String


_

Public Property Id() As Integer

Get

Return _Id

End Get


Set(ByVal value As Integer)

_Id = value

End Set

End Property


_

Public Property name() As String

Get

Return _name

End Get


Set(ByVal value As String)

_name = value

End Set

End Property


_

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.)

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:

For the full article please visit LinQhelp.com. Thanks and happy coding!

http://www.linqhelp.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Protected with IP Blacklist CloudIP Blacklist Cloud