Using LINQPad with an Existing Database

Using LINQPad with an Existing Database


LINQPad’s ability to connect to existing databases simplifies query writing, execution, and result analysis for .NET developers. This article provides a comprehensive guide on how to connect LINQPad to an existing database, allowing developers to leverage its full potential in database management and query execution.

Understanding Database Connectivity in LINQPad

LINQPad supports a wide range of databases, including Microsoft SQL Server, MySQL, Oracle, PostgreSQL, SQLite, and more.

It facilitates direct database connections, allowing developers to perform various operations such as executing queries, exploring schemas, and analysing data directly within the tool.

Key Benefits of Database Connectivity in LINQPad

  • Simplified Connection Setup: Intuitive interface for establishing database connections.
  • Integrated Query Execution: Run SQL and LINQ queries against connected databases.
  • Schema Browsing: Conveniently browse and understand database structures.
  • Data Visualisation: Advanced data formatting and visualisation options.
  • Performance Insights: Tools for analysing and optimising query performance.

Step 1: Setting Up a Database Connection

Selecting the Database Type

  1. Open LINQPad and click on ‘Add connection’ (usually found in the top left corner).
  2. Choose the type of database you want to connect to (e.g., SQL Server, MySQL). LINQPad will prompt you to download the appropriate driver if it’s not already installed.

Configuring the Connection

  1. Connection String: Enter the connection string for your database. This typically includes the server name, database name, and authentication details.
  2. Authentication: Choose between Windows Authentication and standard Database Authentication, depending on your database setup.
  3. Testing the Connection: Use the ‘Test’ button to verify that LINQPad can connect to your database.

Saving the Connection

  • Once the connection is successful, name the connection for future reference and click ‘Save’.

Step 2: Exploring the Database

Upon successful connection, LINQPad displays the database schema. You can explore tables, views, stored procedures, and functions. This exploration feature is particularly helpful for understanding the database structure and planning your queries.

Step 3: Writing and Executing Queries

Executing LINQ Queries

Once you’ve successfully connected LINQPad to your database, the next powerful feature to explore is executing LINQ queries. LINQ (Language Integrated Query) is a robust language feature of .NET that allows you to write expressive code to query and manipulate data. LINQPad elevates this experience by providing a dynamic environment where you can write, test, and refine your LINQ queries with immediate feedback.

Crafting Your Query

  • Open the query window in LINQPad.
  • Select the language of your choice (C#, VB, etc.) at the bottom right of the window.
  • Start typing your LINQ query. LINQPad’s intelligent code completion feature assists you by suggesting methods, properties, and keywords, making your coding process faster and more accurate.

Query Examples

Simple Query: Retrieve a list of customers from a Customers table.

var customers = from customer in db.Customers
                select customer;

Filtered Query: Fetch orders from the last month.

var recentOrders = db.Orders.Where(order => order.Date > DateTime.Now.AddMonths(-1));

Join Query: Combine data from multiple tables.

var customerOrders = from order in db.Orders
                     join customer in db.Customers on order.CustomerId equals customer.Id
                     select new { customer.Name, order.OrderDate };

Executing the Query

  • After writing your query, execute it by pressing F5 or clicking the ‘Run’ button.
  • LINQPad processes your query against the connected database and displays the results in the output pane.

Analyzing the Results

  • The output pane in LINQPad provides a richly formatted view of your query results. It’s interactive, allowing you to drill down into collections and objects.
  • For tabular data, LINQPad presents the results in a grid format, making it easy to browse and understand the data.

Modifying and Re-running Queries

  • LINQPad’s dynamic environment allows you to modify and re-run queries instantly. This is particularly useful for iterative query development and debugging.
  • You can experiment with different LINQ operators and methods to refine your query and see the immediate impact of changes.

Advanced Query Features

  • Aggregation and Grouping: Perform complex operations like sum, average, group by, etc., directly within your queries.
  • Lazy Loading: LINQPad supports lazy loading, meaning it only retrieves data as it’s needed, which is efficient for handling large datasets.
  • Immediate Window: For quick experiments or calculations, use LINQPad’s immediate window for LINQ expressions and C# statements.

Executing SQL Queries

While LINQPad is renowned for its LINQ capabilities, it’s equally adept at handling raw SQL queries. This feature is particularly beneficial for developers who are more comfortable with traditional SQL syntax or need to work with complex queries that might be cumbersome or inefficient to express in LINQ. Here’s how to make the most of this feature:

Switching to SQL Mode

  1. Selecting SQL Mode: In the LINQPad interface, you can easily switch to SQL mode. This is typically done by selecting ‘SQL’ from the language dropdown menu at the bottom right of the query window.
  2. Query Window Transformation: Once you switch to SQL mode, the query window is ready to accept raw SQL queries. The environment adjusts to facilitate a SQL-centric scripting experience.

Writing SQL Queries

  • SQL Query Syntax: Write your SQL query as you would in any database management tool. LINQPad supports standard SQL syntax, including SELECT, INSERT, UPDATE, DELETE, and more complex operations like JOINs, subqueries, and stored procedure calls.
  • Autocompletion and Syntax Highlighting: LINQPad aids in writing SQL queries by providing autocompletion for table names, columns, and SQL keywords, along with syntax highlighting, making it easier to write error-free queries.

Example Queries:

Simple SELECT Query:

SELECT * FROM Customers WHERE City = 'London';

JOIN Query:

SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

Aggregation Query:

SELECT COUNT(ProductID), CategoryID
FROM Products
GROUP BY CategoryID;

Executing SQL Queries

  • Running the Query: Execute your SQL query by pressing F5 or clicking the ‘Run’ button. LINQPad communicates with the connected database and executes your SQL command.
  • Results Display: The results are displayed in the output pane, just like LINQ query results. For SELECT queries, results appear in a tabular format, while for other types of queries (like INSERT or UPDATE), LINQPad shows the number of affected rows.

Analyzing and Optimizing SQL Queries

  • Execution Plan Analysis: For complex queries, LINQPad can display the execution plan, helping you understand and optimise the query performance.
  • SQL Profiling: LINQPad’s SQL profiling tools can be used to monitor and analyze the performance of your SQL queries, identifying potential bottlenecks and inefficiencies.

Benefits of SQL Query Execution in LINQPad

  • Flexibility and Choice: Whether you prefer LINQ or SQL, LINQPad caters to both, providing a flexible environment for database querying and manipulation.
  • Rapid Testing and Prototyping: Quick execution and result visualisation make LINQPad an ideal tool for testing and prototyping SQL queries.
  • Integrated Development Experience: The ability to switch between LINQ and SQL queries within the same tool streamlines the development process, especially when working on complex applications that require both types of queries.

Advanced Features

  • Stored Procedures: Run stored procedures directly from LINQPad.
  • Updates and Inserts: Perform data manipulation operations like insert, update, and delete.
  • Custom Visualisers: Use or create custom visualisers for enhanced data representation.

Tips for Efficient Database Operations in LINQPad

  • Autocompletion: Utilise LINQPad’s autocompletion for faster query writing.
  • Reusable Scripts: Save frequently used queries for quick access.
  • Performance Profiling: Employ LINQPad’s performance tools to optimise your queries.


LINQPad is an essential asset for .NET developers, revolutionising the way we interact with databases. This tool isn’t just about executing LINQ queries; it’s a comprehensive solution for streamlined database connectivity, efficient management, and executing complex SQL queries. By integrating these functionalities, LINQPad enhances overall productivity and offers a cohesive workflow for diverse database tasks.

Streamlined Database Connectivity and Management

The ability to connect seamlessly to a variety of databases is one of LINQPad’s standout features. Whether it’s SQL Server, MySQL, Oracle, or others, LINQPad simplifies the connection process. Once connected, developers can dive into efficient data exploration, leveraging LINQPad’s intuitive interface for browsing database schemas, tables, and stored procedures. This streamlined approach is not just a time-saver but also an avenue for deeper insights into database structures and relationships.

Enhanced Productivity in Query Execution and Analysis

LINQPad excels in providing a dynamic environment for query execution and performance analysis. Whether you’re working on refining a LINQ query, debugging complex data retrieval logic, or prototyping new database operations, LINQPad offers the tools and flexibility needed for these tasks. The immediate feedback and rich output formatting significantly reduce development cycles, allowing for quicker iterations and more effective problem-solving.

SQL Query Execution: Complementing LINQ’s Capabilities

Beyond its LINQ capabilities, LINQPad’s prowess in handling raw SQL queries is a game-changer. This feature is a boon for developers who prefer the familiarity and precision of SQL. It’s ideal for executing rapid query tests, running intricate SQL scripts, and optimizing performance. The SQL mode in LINQPad provides a powerful platform for traditional database operations, making it an invaluable component of the .NET developer’s toolkit.

Flexibility for a Diverse Range of Database Operations

LINQPad’s combination of database connectivity, LINQ, and SQL query execution creates a versatile environment suitable for a wide array of database-related tasks. Whether you’re involved in data analysis, application development, or database optimization, LINQPad adapts to your needs, offering a robust and efficient solution. The tool’s adaptability makes it perfect for tasks ranging from simple data retrieval to complex query analysis and optimisation.

LINQPad is a unified solution for .NET developers, bridging the gap between database connectivity, management, and query execution. Its integration of LINQ and SQL capabilities, combined with an intuitive interface for database interaction, makes it a powerhouse tool. By leveraging LINQPad, developers can ensure a more streamlined, insightful, and productive development experience, fitting for the diverse challenges of modern software development.

You can download it from here: –


Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.