Introduction

This article helps us to learn about “LINQ to SQL” and takes you through the step by step process to explain how to create the LINQ to SQL using Visual Studio and also how toachieve the basic Insert, Select, Update and delete operations using the concept of LINQ to SQL.

LINQ to SQL

We have seen the option of “LINQ to SQL” in visual studio 2010 onwards, many may not use it and some may get the chance to use it. “L2S” – [LINQ to SQL] is an Object Relational Mapping [ORM - like other frameworks], which is used to create the strongly typed models automatically with the .net classes based on SQL data tables of the refereed or strongly typed databases. That's simply to say if you provide the Database and map it using LINQ to SQL, then this framework will provide you the classes with properties similar to the data tables.

We can use this LINQ to SQL and can achieve the CRUD operation like Select, Insert, Update, Delete using C# language.

Sample LINQ to SQL

Please follow the steps and try the basic operation with your database tables,

Step 1 - Do right click on your project and choose new item option,

new

Step 2 - In the template wizard choose “LINQ to SQL Classes” template and name it as you wish

template

Step 3 - Open server explorer from view,

explorer

Step 4 - Connect your database and expand it,

database

Step 5 - Select all your database tables and place inside your dbml file. Just drag all the tables and place into your dbml file,

tables

Step 6 - Now you can see all the tables in dbml file with relationships,

file

Sample Code

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace DataAccessLayer
  4. {
  5. public class Sample
  6. {
  7. //Insert method
  8. public void Insert(string data)
  9. {
  10. LINQToSQLDataContext dataObject = new LINQToSQLDataContext();
  11. dataObject.TPG_TeamDetails.InsertOnSubmit(
  12. new TPG_TeamDetail
  13. {
  14. InternalProjectID = 1,
  15. Role = "Dummy",
  16. TeamMemberID = 2,
  17. UserID = 2
  18. });
  19. dataObject.SubmitChanges();
  20. }
  21. // Delete method
  22. public void Delete(int teamMemberID)
  23. {
  24. LINQToSQLDataContext dataObject = new LINQToSQLDataContext();
  25. TPG_TeamDetail TPG_TeamDetailDO =
  26. dataObject.TPG_TeamDetails.Where(p => p.TeamMemberID == teamMemberID).First();
  27. dataObject.TPG_TeamDetails.DeleteOnSubmit(TPG_TeamDetailDO);
  28. dataObject.SubmitChanges();
  29. }
  30. // Select method
  31. public List<UserData> Select()
  32. {
  33. LINQToSQLDataContext dataObject = new LINQToSQLDataContext();
  34. return
  35. (from s in dataObject.TPG_TeamDetails
  36. select new UserData { UserID = s.UserID }
  37. ).ToList<UserData>();
  38. }
  39. // Update method
  40. public void Update(int teamMemberID, int internalProjectID, int userID)
  41. {
  42. LINQToSQLDataContext dataObject = new LINQToSQLDataContext();
  43. TPG_TeamDetail teamDetailDO =
  44. dataObject.TPG_TeamDetails.Where(p => p.TeamMemberID == teamMemberID).First();
  45. teamDetailDO.InternalProjectID = internalProjectID;
  46. teamDetailDO.UserID = userID;
  47. dataObject.SubmitChanges();
  48. }
  49. }
  50. public class UserData
  51. {
  52. public int UserID { get; set; }
  53. }
  54. }
Conclusion

Hope this may have helped you to try the simple application for achieving the basic operation with the database table you have referred from the SQL.