.NET Blog

Tony Cavaliere

 
My Favourite Albums
  And the Grappa wins.
E-mail me Send mail
Add to Technorati Favorites AddThis Feed Button

Subscribe to Cynot Why Not


Recent posts

Disclaimer

Hey unlike other bloggers I stand by what I say but just in case. The opinions expressed herein are my own except on Tuesday when the second card is not turned up otherwise it ain't worth squat.

© Copyright 2010

LINQ to SQL versus ADO, Let's get ready to rumble.

Language Integrated Query (LINQ) was introduced in VS 2008 and is a great way to query data. It is a standard query syntax that essentially provides a common way of querying data regardless of whether the data source in SQL, XML or in-memory objects.

Prior to .NET 3.5 developers would typically use ADO to query relational databases, specifically, if you were querying SQL Server you would be using the SQL ADO libraries. If you wanted to retrieve the data as quickly as possible then you used DataReaders, but it was the developer's responsibility to maintain the data. If performance was not an issue then developers would typically opt for Datasets, a more feature rich mechanism for retrieving and storing the data.

But how performant (I know it's not a word but ...) is LINQ to SQL? I decided to put it to the test.

I wrote a console application to test the performance of LINQ to SQL, ADO Datasets and ADO DataReaders. The application consists of 3 methods one to run LINQ to SQL, one for Datasets and the last for DataReaders. This code is shown in Listing 1. In order to level the playing field the DataReader method populates a custom object, MyCustomer. The code for this class is also included in the listing. In each case we are querying the customer table (91 rows), 10,000 times.

I am using the northwind database and SQLExpress.

Listing 1:

 

Imports System.Data

Imports System.Data.SqlClient

Imports System.Configuration

 

Module Module1

 

    Sub Main()

 

        Dim start As DateTime

        Dim [end] As DateTime

 

        start = DateTime.Now

        GetUsingLINQ()

        [end] = DateTime.Now

        ElapsedTime(start, [end], "Using LINQ")

 

        start = DateTime.Now

        GetUsingDataset()

        [end] = DateTime.Now

        ElapsedTime(start, [end], "Using ADO Dataset")

 

        start = DateTime.Now

        GetUsingDataReader()

        [end] = DateTime.Now

        ElapsedTime(start, [end], "Using ADO DataReader")

 

        System.Console.ReadLine()

 

    End Sub

 

    Private Sub ElapsedTime(ByVal s As DateTime, ByVal e As DateTime, ByVal msg As String)

        System.Console.WriteLine(msg + ": " + ((e.Ticks - s.Ticks) / 10000000.0).ToString + " secs")

    End Sub

 

    Private Sub GetUsingLINQ()

        Dim db As New NorthwindDataContext()

        Dim query = From c In db.Customers

        Dim i As Integer

        For i = 0 To 10000

            query.ToList()

        Next

    End Sub

 

    Private Sub GetUsingDataset()

 

        Dim cstrcol As ConnectionStringSettingsCollection = ConfigurationManager.ConnectionStrings

        Dim cs As String = cstrcol("LINQ2SQLvsADO.My.MySettings.NorthwindConnectionString").ToString

        Dim cn As New SqlConnection(cs)

 

        Dim query As String = _

            "select [CustomerID], [CompanyName], [ContactName], [ContactTitle], " + _

            "[Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax] " + _

            "from [dbo].[Customers]"

 

        Dim cmd As New SqlCommand(query, cn)

        Dim da As New SqlDataAdapter(cmd)

        Dim ds As New DataSet

 

        Dim i As Integer

        For i = 0 To 10000

            da.Fill(ds)

        Next

 

    End Sub

 

    Private Sub GetUsingDataReader()

 

        Dim cstrcol As ConnectionStringSettingsCollection = ConfigurationManager.ConnectionStrings

        Dim cs As String = cstrcol("LINQ2SQLvsADO.My.MySettings.NorthwindConnectionString").ToString

        Dim cn As New SqlConnection(cs)

 

        Dim query As String = _

            "select [CustomerID], [CompanyName], [ContactName], [ContactTitle], " + _

            "[Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax] " + _

            "from [dbo].[Customers]"

 

        Dim cmd As New SqlCommand(query, cn)

        Dim dr As SqlDataReader

        cn.Open()

 

        Dim mc As New MyCustomer

 

        Dim i As Integer

        For i = 0 To 10000

            dr = cmd.ExecuteReader

            While dr.Read

                mc.CustomerID = dr("CustomerID").ToString

                mc.CompanyName = dr("CompanyName").ToString

                mc.ContactName = dr("ContactName").ToString

                mc.ContactTitle = dr("ContactTitle").ToString

                mc.Address = dr("Address").ToString

                mc.City = dr("City").ToString

                mc.PostalCode = dr("PostalCode").ToString

                mc.Country = dr("Country").ToString

                mc.Phone = dr("Phone").ToString

                mc.Fax = dr("Fax").ToString

                mc.Region = dr("Region").ToString

            End While

            dr.Close()

        Next

 

    End Sub

 

 

End Module

 

Public Class MyCustomer

 

    Private _CustomerID As String

    Public Property CustomerID() As String

        Get

            Return _CustomerID

        End Get

        Set(ByVal value As String)

            _CustomerID = value

        End Set

    End Property

    Private _CompanyName As String

    Public Property CompanyName() As String

        Get

            Return _CompanyName

        End Get

        Set(ByVal value As String)

            _CompanyName = value

        End Set

    End Property

    Private _ContactName As String

    Public Property ContactName() As String

        Get

            Return _ContactName

        End Get

        Set(ByVal value As String)

            _ContactName = value

        End Set

    End Property

    Private _ContactTitle As String

    Public Property ContactTitle() As String

        Get

            Return _ContactTitle

        End Get

        Set(ByVal value As String)

            _ContactTitle = value

        End Set

    End Property

    Private _Address As String

    Public Property Address() As String

        Get

            Return _Address

        End Get

        Set(ByVal value As String)

            _Address = value

        End Set

    End Property

    Private _City As String

    Public Property City() As String

        Get

            Return _City

        End Get

        Set(ByVal value As String)

            _City = value

        End Set

    End Property

    Private _Region As String

    Public Property Region() As String

        Get

            Return _Region

        End Get

        Set(ByVal value As String)

            _Region = value

        End Set

    End Property

    Private _PostalCode As String

    Public Property PostalCode() As String

        Get

            Return _PostalCode

        End Get

        Set(ByVal value As String)

            _PostalCode = value

        End Set

    End Property

    Private _Country As String

    Public Property Country() As String

        Get

            Return _Country

        End Get

        Set(ByVal value As String)

            _Country = value

        End Set

    End Property

    Private _Phone As String

    Public Property Phone() As String

        Get

            Return _Phone

        End Get

        Set(ByVal value As String)

            _Phone = value

        End Set

    End Property

    Private _Fax As String

    Public Property Fax() As String

        Get

            Return _Fax

        End Get

        Set(ByVal value As String)

            _Fax = value

        End Set

    End Property

 

End Class

Any guesses on which was faster, which was slowest? The results are in:

  •  
    • LINQ to SQL (12 seconds)
    • ADO Datasets (18 seconds)
    • ADO DataReader (8.9 seconds)

So LINQ is not quite as quick as DataReader but is faster than Datasets.

If you would like to try this out for yourself then cut paste the code above and add it to a module file. In addition you will need to add a LINQ to SQL Class and make sure you call it Northwind.dbml. The only table that is required is the Customers table

 

Guess the movie

Capricorn 15's. Born 2244. Enter the Carousel. This is the time of renewal.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: LINQ
Posted by CynotWhyNot on Friday, April 18, 2008 2:55 PM
Permalink | Comments (19) | Post RSSRSS comment feed

Related posts

Comments

long island dj's us

Monday, November 16, 2009 3:17 AM

long island dj's

This is niche your post has been very helpfull


Regards
Macklin


buenos aires apartment rental us

Monday, November 16, 2009 3:31 AM

buenos aires apartment rental

Nice information need to know more.


Regards
Rohr


collection agency us

Monday, November 16, 2009 5:57 AM

collection agency

Great Work


Regards
Rolonirst

watch true blood online us

Tuesday, November 17, 2009 5:07 AM

watch true blood online

i would love to read more from you on this


Regards
Donnelly

Forex Systems us

Wednesday, November 25, 2009 8:14 AM

Forex Systems

I often read your blog and always find it very interesting. Thought it was about time i let you know�Keep up the great work

Regards
jenny martin


chemical free pools us

Monday, December 14, 2009 12:55 AM

chemical free pools

Thank you very much for the information provided! I was looking for this data for a long time, bit I was not able to find the trusted source

Regards
Gonzalez


pets carriers us

Saturday, December 19, 2009 1:20 AM

pets carriers

Hi webmaster, commenters and everybody else !!! The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!b Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do!



Regards and respect
Hufnagel







how to play craps us

Thursday, January 07, 2010 8:37 AM

how to play craps

Don't listen to mean people. You are awesome and you have a huge heart.

pet potty us

Tuesday, February 09, 2010 11:41 AM

pet potty

Hmm singular this instal is totaly digressive to the look ask I entered in google but it was recorded on the original attender.

Regards
jenny

alta white teeth whitening us

Friday, February 19, 2010 11:28 AM

alta white teeth whitening

The great thing in the world is not so much where we stand, as in what direction we are moving.

nike air yeezy US

Friday, May 28, 2010 10:41 PM

nike air yeezy

Like your writing! Still you can do some things to improve it.

louis vuitton bags US

Monday, May 31, 2010 1:34 PM

louis vuitton bags

What a great info, thank you for sharing. this will help me so much in my learning.

wallets US

Wednesday, July 14, 2010 12:41 PM

wallets

There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game.

max 2010 US

Saturday, July 24, 2010 9:51 AM

max 2010

Great website...and cool article man...thanx for the great post...keep on posting such article... While canadian animals and wildlife are not quite as diverse as their American neighbors, Canada is still one of the most spectacular, all-encompassing wildlife regions on the planet.

air yeezy US

Wednesday, July 28, 2010 1:52 PM

air yeezy

Thanks for your article.

air max 1 shoes US

Monday, August 16, 2010 9:48 AM

air max 1 shoes

Thanks for your article.

nike air max 90 US

Tuesday, August 24, 2010 8:06 PM

nike air max 90

I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful.

designer sandals for women US

Wednesday, August 25, 2010 12:19 PM

designer sandals for women

Hello, I was researching the net and I ran into your blog. Keep up the great work. If you are like most people, you certainly want more energy to deal with daily work and running around.

gucci bags online US

Friday, September 03, 2010 12:12 AM

gucci bags online

Your blog seems interesting.Regards,Kevin.

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Saturday, September 04, 2010 1:39 AM