SIGN UP MEMBER LOGIN:    
ARTICLE

DataContext in LINQ

Posted by Dhananjay Kumar Articles | LINQ with C# June 25, 2011
To communicate with a Database a connection must be made. In LINQ this connection is created by a DataContext.
Reader Level:


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

Essentially a DataContext class performs the following three tasks:

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

    Untitled-8.gif

You can say, it acts exactly the same as the SqlConnection class but performs some extra tasks as well, like conversion of objects to SQL queries.

A DataContext class has four types of overloaded constructor.

Untitled-9.gif
It may take:
  1. Connection string
  2. IDbConnection etc

Various public methods of the DataContext class help us to perform the following tasks:
  1. Create data base
  2. Delete data base etc

You can create and drop a database like below:

Create database
Untitled-11.gif
Delete database
Untitled-12.gif
The 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)
            {
                context.DeleteDatabase();
                Console.WriteLine("Database deleted");
            }
            else
            {
                context.CreateDatabase();
                Console.WriteLine("Database created");
            }
            Console.ReadKey(true); 
        } 
        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";
            Console.WriteLine(builder.ConnectionString);
            Console.ReadKey(true);
            return builder.ConnectionString; 
        }
    }
}


Strongly Typed Data Context

A Strongly typed data context can be created by the following steps:
  1. Create a class to represent a strongly type data context
  2. Inherit the class from the DataContext class.

    Untitled-13.gif

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

I hope this article was useful. Thanks for reading.

Login to add your contents and source code to this article
share this article :
post comment
 

Hi i have a situation like in var a i have stored ID's and i want that var b collection remove all items which matches thus ID's plz help

Posted by Dwarkesh May 04, 2012
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor