LINQ to SQL Tutorial
Lección 3 de 38 · Part 3 How to view LINQ to SQL generated SQL queries · Bases de datos
Part 3 How to view LINQ to SQL generated SQL queries
Text version of the video
http://csharp-video-tutorials.blogspot.com/2014/09/part-3-how-to-view-linq-to-sql.html
Slides
http://csharp-video-tutorials.blogspot.com/2014/09/part-3-how-to-view-linq-to-sql_5.html
LINQ to SQL Tutorial – All Text Articles & Slides
http://csharp-video-tutorials.blogspot.com/2014/09/linq-to-sql-tutorial.html
LINQ to SQL Tutorial Playlist
Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd
In this video we will discuss, how to view the SQL queries generated by LINQ to SQL. For debugging, it is necessary to view the generated SQL statements. There are several ways to do this.
The easiest way is to write the generated sql query to the asp.net page or a console window if it’s a console application.
Writing generated SQL query to the asp.net page
using (SampleDataContext dbContext = new SampleDataContext())
{
// Write the generated sql query to the webform
dbContext.Log = Response.Output;
// Write the generated sql query to the Console window
// dbContext.Log = Console.Out;
var linqQuery = from employee in dbContext.Employees
select employee;
GridView1.DataSource = linqQuery;
GridView1.DataBind();
}
ToString() method can also be used to view the generated sql query
using (SampleDataContext dbContext = new SampleDataContext())
{
var linqQuery = from employee in dbContext.Employees
select employee;
string sqlQuery = linqQuery.ToString();
Response.Write(sqlQuery);
GridView1.DataSource = linqQuery;
GridView1.DataBind();
}
GetCommand() method of DataContext object can also be used
using (SampleDataContext dbContext = new SampleDataContext())
{
var linqQuery = from employee in dbContext.Employees
select employee;
Response.Write(dbContext.GetCommand(linqQuery).CommandText);
Response.Write(“[br/]”);
Response.Write(dbContext.GetCommand(linqQuery).CommandType);
GridView1.DataSource = linqQuery;
GridView1.DataBind();
}
Finally, we can also use SQL profiler.