LINQ to SQL in C#
LINQ to SQL in C#
In our previous articles, we have already understood what the LINQ is. Today we will talk about LINQ to SQL. We know we use LINQ to access data from multiple data sources like objects, XML, data sets, SQL servers etcetera.
Previously Microsoft developed SQL (Structured Query Langauge) to access data from a particular database management system called SQL Server. But the problem with SQL was that it could only work with SQL Server. If you would like to use SQL with other database management software like Oracle, you can not do that because SQL is not developed to work with them. This issue was recognized by Microsoft, this is why they developed LINQ. The LINQ was developed keeping in mind that it will have to work with a wide variety of data sources.
Flavors of LINQ
Because LINQ is a unified data access technology, it has different flavors to work with different objects. I will be explaining all the flavors one by one.
- LINQ to Objects
- LINQ to SQL
- LINQ to Entities
- LINQ to ADO.Net
- LINQ to XML
LINQ to Object
If you remember our last article where we used LINQ with an array and we took out the elements smaller than 20. That was an example of LINQ to object. When we have an array or collections as a data source, we use LINQ to Object.
LINQ to SQL
LINQ to SQL was launch with .Net Framework 3.5 and it was launch to work with RDBMS (Relational Database Management System). One Example if RDBMS is SQL Server. The drawback of LINQ is that it only works with one RDBMS system and that is SQL Server. It can not work with other relational database management systems like Oracle and others.
Note – We can also work with Stored procedures in LINQ to SQL.
RDBMS in Short
If you are confused about what is RDBMS then be informed it is a big concept and can not be explained in this article. For now, you can think of RDBMS as a database where its tables are connected to each other, using Foreign keys. This means a bunch of tables work together and form a big database.
LINQ to Entities
The LINQ to Entities is very similar to LINQ to SQL. It is also designed to work with RDBMS. The only difference is that LINQ to Entities can work with multiple RDBMS like Oracle Server, SQL Server etcetera.
LINQ to ADO.Net
As we already know ADO.Net is an old technology of Microsoft to deal with databases and perform desired operations. You have two options when using ADO.Net. Either you can use SQL queries with ADO.Net or you can use LINQ Queries. You might already you that ADO.Net provides you classes, in which you can put the SQL query and perform the desired task on the database.
When you write an SQL query inside the class you put that query in inverted commas (” “). That means the query goes to the ADO.Net as a string and the ADO.Net engine runs it. In this operation, you might make a mistake in SQL syntax and there is no mechanism in C# to check the syntax because the query is a string.
Ultimately the syntax will be checked by the ADO.Net engine and you will get an error from ADO.Net. In this case, the ADO.Net engine has this extra responsibility.
If you write a LINQ to query the compiler of C# can correct you and will not let the program run until the syntax is corrected. This will save the ADO.Net engine from taking a lot of unnecessary burdens.
LINQ to XML
As the name explains, this is used to work with the data saved in XML.
Be informed, when we talk about the flavors can work with SQL Servers, Oracle, and Objects and all. It does not mean only the selection of data. It means CRUD operations. If you do not know what is CRUD.
These are four types of operations that we perform on data sources. In any case, you will be writing one of these types of queries.
- In case you want to save some data in the database that means you want to run create operations.
- In case of selecting some data from the database, that means you want to perform Read operations.
- Making changes into the data means update operations are required.
- Want to delete some data from the database? you need to run a delete query.
I hope, the article was helpful. Feel free to give feedback in comment box.
Take care guys!