Home > Linq > LINQ # 5 – LINQ to SQL (Part II) (Queries using LINQ to SQL classes)

LINQ # 5 – LINQ to SQL (Part II) (Queries using LINQ to SQL classes)

Hi Geeks,

As we saw in our last post LINQ to SQL how to create a LINQ to SQL classes and how to use Entity class to represent database tables.

Today we will see how to Fire basic sql queries  against database using the Entity classes.

Suppose we have a table in database named tblStudent as follows

image

LINQtoSQL Code Examples

1)Select all the records from tblStudent

     public static void GetStudents()
       
{
           
LinqToSqlDataContext objDataContext = new LinqToSqlDataContext();
           
var students = from student in objDataContext.tblStudents
                                  
select student;
       
}
 

2)Insert record into the tblStudent

      public static void AddStudents()
       
{
           
LinqToSqlDataContext objDataContext = new LinqToSqlDataContext();                      
             tblStudent objStudent = new tblStudent();
            objStudent.Student_Age = 30;
            objStudent.Student_Name = “XYZ”;
          
            //To add the record in to the table
            objDataContext.tblStudents.InsertOnSubmit(objStudent);
           
//To reflect changes in database
            objDataContext.SubmitChanges();
        
          }
 

3)Update record into the tblStudent

      Suppose we want to update the record by StudentId

      public static void UpdateStudents(int StudentId)
       
{
           
LinqToSqlDataContext objDataContext = new LinqToSqlDataContext();

            //Select the row want to update 

           tblStudent objStudent = objDataContext.tblStudents.SingleOrDefault 
          
(c => c.Student_Id == StudentId);  

             
           
//Update the name to PQR
           objStudent.Student_Name = “PQR”;
           
           
//To update the record in to the table
            objDataContext.SubmitChanges();//To reflect changes in database
        }
 

4)Delete record  from tblStudent

 public static void DeleteStudent(int StudentId)
       
{
             
LinqToSqlDataContext objDataContext = new LinqToSqlDataContext();
                     
             
tblStudent objStudent = objDataContext.tblStudents.
              SingleOrDefault(c => c.Student_Id == StudentId);

              objDataContext.tblStudents.DeleteOnSubmit(objStudent);
             
objDataContext.SubmitChanges();
        
       
}

Hope this will help you in doing basic INSERT,UPDATE,DELETE,SELECT operation using

LINQ to SQL classes.

Thanks.

Advertisements
Categories: Linq
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: