Beginning LINQ to SQL Using Visual Studio 2008 - Developing a simple LINQ to SQL application (without LINQ designer): UI Design and source in VB.NET
(Page 2 of 6 )
The following steps are continued from the previous section.
Add a reference to "System.Data.Linq" (Go to Project || Add Reference)
Add a new class (Project || Add class) to the project and name it "Emp.vb"
Copy the following code into it:
Imports System.Data.Linq.Mapping
<Table(Name:="emp")> _
Public Class Emp
Private _empno As Integer
<Column(IsPrimaryKey:=True)> _
Public Property empno() As Integer
Get
Return _empno
End Get
Set(ByVal value As Integer)
_empno = value
End Set
End Property
Private _ename As String
<Column()> _
Public Property ename() As String
Get
Return _ename
End Get
Set(ByVal value As String)
_ename = value
End Set
End Property
Private _sal As Nullable(Of Double)
<Column()> _
Public Property Sal() As Nullable(Of Double)
Get
Return _sal
End Get
Set(ByVal value As Nullable(Of Double))
_sal = value
End Set
End Property
Private _deptno As Nullable(Of Integer)
<Column()> _
Public Property deptno() As Nullable(Of Integer)
Get
Return _deptno
End Get
Set(ByVal value As Nullable(Of Integer))
_deptno = value
End Set
End Property
End Class
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSelectStar" runat="server" Text="Select *" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
Imports System.Data.Linq
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub btnSelectStar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSelectStar.Click
Dim db As New DataContext(New System.Data.SqlClient.SqlConnection("data source=.sql2k5;initial catalog=Sample;user id=sa;Password=eXpress2005"))
Dim tblEmps As Table(Of Emp) = db.GetTable(Of Emp)()
Me.GridView1.DataSource = tblEmps
Me.GridView1.DataBind()
End Sub
End Class
Once executed (pressing F5), you should be able to see the output as follows (Fig 4):

Next: Developing a simple LINQ to SQL application (without LINQ designer): source in C# >>
More .NET Articles
More By Jagadish Chaterjee