LINQ To SQL Selecting mulitple fields in a database table

By bryan tugade

We can select multiple fields in a given database tables using Linq to sql. I use AdventureWorks as my database in this sample app and to show you how it works i bind the results in gridview control.

Imports System.Data.Linq

Partial Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim adventureContext = New adventureWorks()

        Try
            Dim xemployee = (From employee In adventureContext.Employees Select employee.Title, employee.MaritalStatus, employee.VacationHours)
             GridView1.DataSource = xemployee
             GridView1.DataBind()

        Catch ex As Exception

           Console.WriteLine(ex.Message)
        
         End Try
    End Sub
End Class

You can add specific fields that you want to show in the result in this line of code. i.e.

Dim xemployee = (From employee In adventureContext.Employees Select employee.Title, employee.MaritalStatus, employee.VacationHours) ' add fields here...

here's the result



That's it! Happy Coding. : )

Related FAQs

In C#, we can select and deselect checkbox in gridview in onPostback event of checkbox in gridview header.
Have you try to transfer your data fro SQL Express to Microsoft office excel? What if your company wants you to get all the data from your database and put it in excel. Theres two way i know how to do it. Here it is.
We can hover on gridview row using javascript onmouseover event. We can also change the color in code behind.
We can filter data using dataview.
This snippet shows how to connect to SQLExpress and execute stored procedure.
Assuming that you need to create an application that needs to transfer data from sql server to microsoft excel. The first thing you need to do is to find a way to connect to sql server. We can do this by using visual basic application.
LINQ To SQL Selecting mulitple fields in a database table  (1359 Views)