Can you explain what is meant by 'clean SQL' and how it differs from ORM methods?
sobes.tech AI
Answer from AI
"Pure SQL" refers to writing and using SQL queries directly, without intermediaries or abstractions such as ORM (Object-Relational Mapping). This means that the developer manually formulates SQL queries to interact with the database.
Differences from ORM:
-
Control and transparency: with pure SQL, you know exactly which queries are executed, making optimization and debugging easier.
-
Flexibility: you can use database-specific features and complex queries that are not always easy to express through ORM.
-
Lack of abstractions: ORM hides SQL details behind objects and methods, simplifying development but sometimes leading to suboptimal queries.
-
Performance: pure SQL can be faster because you write exactly the query you need, without extra layers.
Example of pure SQL in C# using ADO.NET:
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
var command = new SqlCommand("SELECT * FROM Users WHERE Age > @age", connection);
command.Parameters.AddWithValue("@age", 18);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader["Name"]);
}
}
}
Unlike ORM, where you might write something like context.Users.Where(u => u.Age > 18), here you control the query directly.