Introduction:
In this article,i am going to explain about the actions between linq api and sql relational database.
Main:
LINQ to SQL is a component of the LINQ project that provides the capability to query a relational Microsoft SQL Server database, offering you an object model based on available entities. In other words, you can define a set of objects that represents a thin abstraction layer over the relational data, and you can query this object model by using LINQ queries that are converted into corresponding SQL queries by the LINQ to SQL engine. LINQ to SQL supports Microsoft SQL Server starting from SQL Server 2000 and Microsoft SQL Server Compact starting from version 3.5.
In LINQ to SQL, you can write a simple query like the following:
var query =
from c in Emps
where c.Name == "James"
&& c.Name == "Peter"
select new {c.Empid, c.Name, c.Designation };
var query = from c in Emps where c.Name == "James" && c.Name == "Peter" select new {c.Empid, c.Name, c.Designation }; |
This query is converted into an SQL query that is sent to the relational database,
Select Empid,Name,Designation from Emp where name="James" and name="Peter";
Select Empid,Name,Designation from Emp where name="James" and name="Peter"; |
For ex,Add a sql to linq class,

Once we drag and dropped the relational table in linq api,the mapping and datacontext is created automatically,

The table object has to be instantiated. To do that, you need an instance of the DataContext class, which defines the bridge between the LINQ world and the external relational database. The nearest concept to DataContext that comes to mind is a database connection—in fact, a mandatory parameter needed to create a DataContext instance is the connection string or the Connection object. Its GetTable
DataContext db = new DataContext("");
Table<Emp> = db.GetTable<Emp>();
DataContext db = new DataContext(""); Table<Emp> = db.GetTable<Emp>(); |
The DataContext class internally uses the SqlConnection class from ADO.NET. You can pass an existing SqlConnection to the DataContext constructor, and you can also read the connection used by a DataContext instance through its Connection property. All services related to the database connection, such as connection pooling (which is turned on by default), are accessible at the SqlConnection level and are not directly implemented in the DataContext class.
Any external data must be described with appropriate metadata bound to class definitions. Each table must have a corresponding class decorated with particular attributes. That class corresponds to a row of data and describes all columns in terms of data members of the defined type. The type can be a complete or partial description of an existing physical table, view, or stored procedure result.
The Linq to sql api will automatically convert the table ad generic class,
here, Table
Auto generated mapping class between linq api and sql class,
#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Linqdd
{
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel;
using System;
[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="master")]
public partial class EmpDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
partial void InsertEmp(Emp instance);
partial void UpdateEmp(Emp instance);
partial void DeleteEmp(Emp instance);
#endregion
public EmpDataContext() :
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["masterConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}
public EmpDataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public EmpDataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public EmpDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public EmpDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public System.Data.Linq.Table<Emp> Emps
{
get
{
return this.GetTable<Emp>();
}
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Emp")]
public partial class Emp : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _Empid;
private string _FirstName;
private string _LastName;
private System.Nullable<int> _Deptid;
private string _DeptName;
private string _Salary;
private string _Manager;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnEmpidChanging(int value);
partial void OnEmpidChanged();
partial void OnFirstNameChanging(string value);
partial void OnFirstNameChanged();
partial void OnLastNameChanging(string value);
partial void OnLastNameChanged();
partial void OnDeptidChanging(System.Nullable<int> value);
partial void OnDeptidChanged();
partial void OnDeptNameChanging(string value);
partial void OnDeptNameChanged();
partial void OnSalaryChanging(string value);
partial void OnSalaryChanged();
partial void OnManagerChanging(string value);
partial void OnManagerChanged();
#endregion
public Emp()
{
OnCreated();
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Empid", DbType="Int NOT NULL", IsPrimaryKey=true)]
public int Empid
{
get
{
return this._Empid;
}
set
{
if ((this._Empid != value))
{
this.OnEmpidChanging(value);
this.SendPropertyChanging();
this._Empid = value;
this.SendPropertyChanged("Empid");
this.OnEmpidChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_FirstName", DbType="VarChar(20)")]
public string FirstName
{
get
{
return this._FirstName;
}
set
{
if ((this._FirstName != value))
{
this.OnFirstNameChanging(value);
this.SendPropertyChanging();
this._FirstName = value;
this.SendPropertyChanged("FirstName");
this.OnFirstNameChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_LastName", DbType="VarChar(20)")]
public string LastName
{
get
{
return this._LastName;
}
set
{
if ((this._LastName != value))
{
this.OnLastNameChanging(value);
this.SendPropertyChanging();
this._LastName = value;
this.SendPropertyChanged("LastName");
this.OnLastNameChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Deptid", DbType="Int")]
public System.Nullable<int> Deptid
{
get
{
return this._Deptid;
}
set
{
if ((this._Deptid != value))
{
this.OnDeptidChanging(value);
this.SendPropertyChanging();
this._Deptid = value;
this.SendPropertyChanged("Deptid");
this.OnDeptidChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_DeptName", DbType="VarChar(20)")]
public string DeptName
{
get
{
return this._DeptName;
}
set
{
if ((this._DeptName != value))
{
this.OnDeptNameChanging(value);
this.SendPropertyChanging();
this._DeptName = value;
this.SendPropertyChanged("DeptName");
this.OnDeptNameChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Salary", DbType="VarChar(20)")]
public string Salary
{
get
{
return this._Salary;
}
set
{
if ((this._Salary != value))
{
this.OnSalaryChanging(value);
this.SendPropertyChanging();
this._Salary = value;
this.SendPropertyChanged("Salary");
this.OnSalaryChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Manager", DbType="VarChar(20)")]
public string Manager
{
get
{
return this._Manager;
}
set
{
if ((this._Manager != value))
{
this.OnManagerChanging(value);
this.SendPropertyChanging();
this._Manager = value;
this.SendPropertyChanged("Manager");
this.OnManagerChanged();
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
#pragma warning restore 1591
#pragma warning disable 1591 //------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // Runtime Version:4.0.30319.1 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace Linqdd { using System.Data.Linq; using System.Data.Linq.Mapping; using System.Data; using System.Collections.Generic; using System.Reflection; using System.Linq; using System.Linq.Expressions; using System.ComponentModel; using System; [global::System.Data.Linq.Mapping.DatabaseAttribute(Name="master")] public partial class EmpDataContext : System.Data.Linq.DataContext { private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource(); #region Extensibility Method Definitions partial void OnCreated(); partial void InsertEmp(Emp instance); partial void UpdateEmp(Emp instance); partial void DeleteEmp(Emp instance); #endregion public EmpDataContext() : base(global::System.Configuration.ConfigurationManager.ConnectionStrings["masterConnectionString"].ConnectionString, mappingSource) { OnCreated(); } public EmpDataContext(string connection) : base(connection, mappingSource) { OnCreated(); } public EmpDataContext(System.Data.IDbConnection connection) : base(connection, mappingSource) { OnCreated(); } public EmpDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base(connection, mappingSource) { OnCreated(); } public EmpDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base(connection, mappingSource) { OnCreated(); } public System.Data.Linq.Table<Emp> Emps { get { return this.GetTable<Emp>(); } } } [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Emp")] public partial class Emp : INotifyPropertyChanging, INotifyPropertyChanged { private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); private int _Empid; private string _FirstName; private string _LastName; private System.Nullable<int> _Deptid; private string _DeptName; private string _Salary; private string _Manager; #region Extensibility Method Definitions partial void OnLoaded(); partial void OnValidate(System.Data.Linq.ChangeAction action); partial void OnCreated(); partial void OnEmpidChanging(int value); partial void OnEmpidChanged(); partial void OnFirstNameChanging(string value); partial void OnFirstNameChanged(); partial void OnLastNameChanging(string value); partial void OnLastNameChanged(); partial void OnDeptidChanging(System.Nullable<int> value); partial void OnDeptidChanged(); partial void OnDeptNameChanging(string value); partial void OnDeptNameChanged(); partial void OnSalaryChanging(string value); partial void OnSalaryChanged(); partial void OnManagerChanging(string value); partial void OnManagerChanged(); #endregion public Emp() { OnCreated(); } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Empid", DbType="Int NOT NULL", IsPrimaryKey=true)] public int Empid { get { return this._Empid; } set { if ((this._Empid != value)) { this.OnEmpidChanging(value); this.SendPropertyChanging(); this._Empid = value; this.SendPropertyChanged("Empid"); this.OnEmpidChanged(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_FirstName", DbType="VarChar(20)")] public string FirstName { get { return this._FirstName; } set { if ((this._FirstName != value)) { this.OnFirstNameChanging(value); this.SendPropertyChanging(); this._FirstName = value; this.SendPropertyChanged("FirstName"); this.OnFirstNameChanged(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_LastName", DbType="VarChar(20)")] public string LastName { get { return this._LastName; } set { if ((this._LastName != value)) { this.OnLastNameChanging(value); this.SendPropertyChanging(); this._LastName = value; this.SendPropertyChanged("LastName"); this.OnLastNameChanged(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Deptid", DbType="Int")] public System.Nullable<int> Deptid { get { return this._Deptid; } set { if ((this._Deptid != value)) { this.OnDeptidChanging(value); this.SendPropertyChanging(); this._Deptid = value; this.SendPropertyChanged("Deptid"); this.OnDeptidChanged(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_DeptName", DbType="VarChar(20)")] public string DeptName { get { return this._DeptName; } set { if ((this._DeptName != value)) { this.OnDeptNameChanging(value); this.SendPropertyChanging(); this._DeptName = value; this.SendPropertyChanged("DeptName"); this.OnDeptNameChanged(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Salary", DbType="VarChar(20)")] public string Salary { get { return this._Salary; } set { if ((this._Salary != value)) { this.OnSalaryChanging(value); this.SendPropertyChanging(); this._Salary = value; this.SendPropertyChanged("Salary"); this.OnSalaryChanged(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Manager", DbType="VarChar(20)")] public string Manager { get { return this._Manager; } set { if ((this._Manager != value)) { this.OnManagerChanging(value); this.SendPropertyChanging(); this._Manager = value; this.SendPropertyChanged("Manager"); this.OnManagerChanged(); } } } public event PropertyChangingEventHandler PropertyChanging; public event PropertyChangedEventHandler PropertyChanged; protected virtual void SendPropertyChanging() { if ((this.PropertyChanging != null)) { this.PropertyChanging(this, emptyChangingEventArgs); } } protected virtual void SendPropertyChanged(String propertyName) { if ((this.PropertyChanged != null)) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } } #pragma warning restore 1591 |
Conclusion:
Hope this helps,
Happy coding.

Nice site, nice and easy on the eyes and great content too.
found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later
I just added your blog site to my blogroll, I pray you would give some thought to doing the same.
What a great resource!
This is such a great resource that you are providing and you give it away for free. I enjoy seeing websites that understand the value of providing a prime resource for free. I truly loved reading your post. Thanks!
Terrific work! This is the type of information that should be shared around the web. Shame on the search engines for not positioning this post higher!