Wriju's BLOG

.NET, Cloud and everything

LINQ to SQL support inserting data through object.

LINQ to SQL support inserting data through object.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data.Linq;


namespace Linq2Sql_demo_doc


    /// <summary>

    /// Class to represent the SQL Server table

    /// Emp

    /// Id int

    /// Name varchar(50)

    /// </summary>    ///


    public class Emp


        //Comlumn mapping used from Data.Linq

        [Column(IsPrimaryKey=true, IsDBGenerated=true)]

        public int Id { get; set; }



        public string Name { get; set; }



    //Class for the DataContext and List generation

    public class TestDB : DataContext


        public Table<Emp> Emps;


        //Initializing base class constructor

        public TestDB(string s) : base(s) { }



    class Program


        static void Main(string[] args)


            string strConnection =

@”Data Source=.\sqlexpress;Initial Catalog=TestDB;”;


            TestDB db = new TestDB(strConnection);


            Emp emp = new Emp();


            emp.Name = “New Employee”;








All the methods are coming from DataContext class (responsible for SQL query generation). The above method converts the object addition to DML query.