LINQ Query Visualizer Sample

The Query Visualizer is an assembly that can be used in a Visual Studio debug session to visualize database queries. The project itself is just a library and cannot be run. If you compile it, a library (DLL) is generated that can be used in Visual Studio to enhance your ability to debug LINQ to SQL applications.

Note

When you are debugging applications by using the visualizer, you may have to add Persist Security Info=true to the connection string. When the debugger is activated, it serializes information from the program being debugged and sends it over to the process which displays the visualizer. One piece of information that is serialized is the connection string which is obtained from the SqlConnection object. By default, the SqlConnection object does not return the password and, therefore, the connection string without the password is serialized and sent to the visualizer. When you try to execute the query in the visualizer, it may fail because it does not have the password. Only use this setting when you debug. We do not recommend this setting for production.

To get samples and instructions for installing them

  • Do one or more of the following:

    • On the Help menu, click Samples.

      The Readme displays information about samples.

    • Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.

    • Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.

For more information, see Visual Studio Samples.

Security noteSecurity Note:

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To run this sample

  1. Open and build the Query Visualizer project.

  2. Copy the DLL produced by the compilation from the bin directory of your project into the Visualizers directory, which by default is located at C:\Users\UserName\Documents\Visual Studio 2008\Visualizers. You may have to create the Visualizers directory. In Windows XP and earlier versions, use the My Documents directory rather than Documents. If you have permissions, you can also copy the file to the following directory:...\Program Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\Visualizers.

  3. If necessary, exit Visual Studio and restart to make sure that the visualizer is recognized

  4. Test the visualizer in an existing sample project, such as SampleQueries or create a new console application in which to test the visualizer. To create a new application, follow these steps:

    1. Create a default console application.

    2. Use the Object Relational Designer to attach to the Northwind database that is located in the samples directory.

    3. Open the Add New Item dialog box (Ctrl-Shift-A) and click LINQ to SQL classes.

    4. Open Server Explorer by pressing Ctrl + W, L.

    5. Click the Connect to Database button in the Server Explorer. If necessary, select Microsoft SQL Server Database File as the Data source. Browse for the Northwind.mdb database in the Data directory that is included with the samples.

    6. Drag the Customers table on to the designer surface.

    7. Write and call a simple query such as the one shown here:

      public void SimpleQuery()
      {
          DataClasses1DataContext db = new DataClasses1DataContext();
          var query = from c in db.Customers
                      select c;
          foreach (var item in query)
              Console.WriteLine(item.CompanyName);
      }
      
    8. Put a breakpoint on the foreach line and run to it.

    9. Hover the move pointer over the variable name query and click the magnifying glass icon that appears.

Demonstrates

The query visualizer is a useful utility that can be run in the debugger.

Note

Many LINQ samples require XML files and/or the version of the Northwind sample database that is located in the Data sample. If you install the Data Sample in the same parent folder as the other LINQ samples, the path for the XML files and database file will be resolved automatically. Many sample files also require the Object Dumper Sample project.

See Also

Other Resources

LINQ C# Samples

Getting Started (LINQ to XML)