Saturday, January 5, 2013

Insert, Update, Delete and Retrieve Example in LINQ


In my previous post i have given you a example of how to use the EntityDataReader class to get a DataTable from a LINQ query. Here i'm posing a example code to insert, Update, Delete and retrieve records using LINQ.

Below is a sample class i have used in one of my applications. You may modify the code as per your needs. Remember you need to have reference the EntityDataReader class in your application for GetContact() method to work.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using AirTouchLibrary.DomainObject;
using System.Transactions;
using Microsoft.Samples.EntityDataReader;
using System.Globalization;

namespace AirTouchLibrary.Repository
{
    class ContactRepository
    {
        #region Variables
        AirTouchDataContext airTouch;
        DataTable DT;

        #endregion

        #region InsertContact

        public string InsertContact(Contact contact)
        {
            try
            {
                airTouch = new AirTouchDataContext(ConString.DBConnection);
                using (TransactionScope transaction = new TransactionScope())
                {
                    tblContact tblcontact = new tblContact();
                    tblcontact.Name = contact.Name;
                    tblcontact.Address = contact.Address;
                    tblcontact.Contact = contact.ContactD;
                    tblcontact.AddedBy = contact.AddedBy;
                    tblcontact.Designation=contact.Designation;
                    //  tblcustomer.Status = customer.status;
                    tblcontact.TimeStamp = DateTime.ParseExact(DateTime.Now.ToShortDateString(), "dd/MM/yyyy", DateTimeFormatInfo.InvariantInfo);
                    airTouch.tblContacts.InsertOnSubmit(tblcontact);
                    airTouch.SubmitChanges();
                    transaction.Complete();
                    return "Contact Added";

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        #endregion

        #region UpdateContat
        public string UpdateContact(Contact contact)
        {
            try
            {
                airTouch = new AirTouchDataContext(ConString.DBConnection);
                using (TransactionScope transaction = new TransactionScope())
                {
                    // tblCustomer tblcustomer = new tblCustomer();
                    var query = (from tblcontact in airTouch.tblContacts
                                 where tblcontact.ContactID ==contact.ContactID
                                 select tblcontact).First();

                    query.Name = contact.Name;
                    query.Address = contact.Address;
                    query.Contact = contact.ContactD;
                    query.AddedBy = contact.AddedBy;
                    query.Designation=contact.Designation;
                    //  tblcustomer.Status = customer.status;
                    query.TimeStamp = DateTime.ParseExact(DateTime.Now.ToShortDateString(), "dd/MM/yyyy", DateTimeFormatInfo.InvariantInfo);
                 
                    airTouch.SubmitChanges();
                    transaction.Complete();
                    return "Contact Updated";

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        #endregion

        #region DeleteContatct
        public string DeleteContact(int contactID)
        {
            try
            {
                airTouch = new AirTouchDataContext(ConString.DBConnection);
                using (TransactionScope transaction = new TransactionScope())
                {
                    // tblCustomer tblcustomer = new tblCustomer();
                    var query = (from tblcontact in airTouch.tblContacts
                                 where tblcontact.ContactID == contactID
                                 select tblcontact).First();

                    airTouch.tblContacts.DeleteOnSubmit(query);
                    airTouch.SubmitChanges();
                    transaction.Complete();
                    return "Contact Deleted";

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        #endregion

        #region getContat
        public DataTable GetContact()
        {
            DT = new DataTable();
            airTouch = new AirTouchDataContext(ConString.DBConnection);
            DT = (from tblcon in airTouch.tblContacts
                  select new
                  {
                      tblcon.ContactID,
                      tblcon.Address,
                      tblcon.Name,
                      tblcon.Contact,
                      tblcon.Designation,
                      tblcon.AddedBy,
                      tblcon.Status,
                      tblcon.TimeStamp

                  }).ToDataTable();
            return DT;
        }

        #endregion
    }
       

    }

No comments:
Write comments
Recommended Posts × +