DataContext in LINQ

To make a communication with Database a connection must be made. In LINQ this connection is created by DataContext.

Essentially Data Context class performs below two tasks

  1. Create connection to database.
  2. It submits and retrieves object to database.
  3. Converts objects to SQL queries and vice versa


You can say, it acts as exactly the same as SqlConnection class and perform some extra tasks as well like conversion of object to SQL query.

DataContext class is having four types of overloaded constructor.


It may take

  1. Connection string
  2. IDbConnection etc

Various public methods of DataContext class help us to perform below tasks

  1. Create data base
  2. Delete data base etc

You can create and drop a database like below,

Create database


Delete database


Full source code is as below,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace ConsoleApplication1
    class Program

        static void Main(string[] args)

            DataContext context = new DataContext(GetConnectionString("Yourservername"));
            bool dbExist = context.DatabaseExists();
            if (dbExist == true)

                Console.WriteLine("Database deleted");
                Console.WriteLine("Database created");


        static string GetConnectionString(string serverName)

            System.Data.SqlClient.SqlConnectionStringBuilder builder =
                           new System.Data.SqlClient.SqlConnectionStringBuilder();
            builder["Data Source"] = serverName;
            builder["integrated Security"] = true;
            builder["Initial Catalog"] = "Sample2";
            return builder.ConnectionString;


Strongly Typed Data Context

Strongly typed Data context can be created by below steps

  1. Create class to represent strongly type data context
  2. Inherits the class from DataContext class.


Advantage of using strongly typed data context is that each table is available in Table collections. So you do not need to fetch tables using GetTable method.

I hope this post was useful. Thanks for reading Smile

Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Yahoo BuzzAdd to Newsvine

Published by

Dhananjay Kumar

Dhananjay Kumar is Developer Evangelist for Infragistics. He is a 8 times Microsoft MVP and well respected Developer Advocate in India.He is the author of 900+ Blog Posts, and can often be found speaking around India at conferences and hosting free workshops for programmers across the country. So far, he has hosted 60 free workshops on various topics like JavaScript, Angular, WCF, ASP.NET MVC, C#, Azure etc. Follow him on twitter @debug_mode for all the updates about his blog posts and workshops. You can send him email at debugmode [at] outlook [dot] com

2 thoughts on “DataContext in LINQ”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s