SQL Builder

Query In Code.


SQL Query Builder for .NET: Unlocking SQL Power

The SQL Query Builder is a straightforward framework that empowers .NET developers to craft SQL-like queries, directly interacting with SQL Server datastores.

It provides a SQL-like flow for writing queries, allowing you to intuitively leverage the full capabilities of your SQL Database. This means you can:

Essentially, it bridges the gap between your program i.e. C#, VB.net, etc. application and the raw power of your SQL Server, making database interactions more intuitive and efficient.

Check the following:

var (sql, parameters) = new SqlQueryBuilder()  
   .Select()  
  .Column("P.ProductName", "ProductName")  
  .Column("S.CompanyName", "SupplierName")  
  .Column("C.CategoryName", "CategoryName")  
  .Column("P.UnitPrice", "UnitPrice")  
  .From("Products", "P")  
   .Join(new List()  
   {  
      new INNERJOIN().TableName("Suppliers","S")  
        .On(new Column("P.SupplierID").Equale(new Column("S.SupplierID"))),  
      new INNERJOIN().TableName("Categories","C")  
        .On(new Column("P.CategoryID").Equale(new Column("C.CategoryID")))  
   }) 
   .Build(); 

Prerequisites

Getting started


Initial Setup

Usage

Below are the requirements to run the following sample code.


Sample Application
using Dapper;
using Microsoft.Data.SqlClient;
using SqlBuilder;

namespace DemoApplication
{
    internal class Program
    {
        static string ConnectionString = "{${northwind database connection string}}";
        static void Main(string[] args)
        {
            var (sql, parameters) = new SqlQueryBuilder()
                .Select().Columns("CustomerID", "CompanyName")
                .Column("ContactName", "Name")
                .Column("ContactTitle", "Title")
                .From("Customers")
                .Where(new Where(new IN(new Column("CustomerID"), "ALFKI", "ANATR", "BERGS", "BOTTM", "CHOPS", "FRANK", "GODOS")))
                .Build();
            List customCustomers = new List();
            using (var connection = new SqlConnection(ConnectionString))
            {
                customCustomers = connection.Query(sql, parameters).ToList();
            }            
            Console.WriteLine("Parameters Used:");
            parameters.Keys.ToList().ForEach(k => 
            {
                Console.WriteLine($"Key: {k}, Value: {parameters[k]}");
            });            
            Console.WriteLine();
            Console.WriteLine("Query Output:");
            customCustomers.ForEach(k => Console.WriteLine(k.ToString()));
            Console.ReadKey();
        }
    }

    public class CustomCustomers
    {
        public string CustomerID { get; set; }
        public string CompanyName { get; set; }
        public string Name { get; set; }
        public string Title { get; set; }

        public override string ToString()
        {
            base.ToString();
            return $"CustomerID: {CustomerID}, CompanyName: {CompanyName}, Name: {Name}, Title: {Title}.";
        }
    }
}