
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:
- Write familiar SQL syntax within your .NET code.
- Optimize queries for performance and efficiency, tapping into SQL Server's native strengths.
- Maintain database control, utilizing advanced SQL features.
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
- Compatible with a wide range of .NET versions
- Supports .NET Framework 4.6.1 and later, .NET Core, and .NET 5/6/7/8
- Suits, both the older .NET Framework projects and newer .NET Core/NET projects
- Any ORM that supports Parameterized SQL Queries, Dapper.Net is ideal and recommended
Getting started
- Using the .NET CLI, open a command line and navigate to the directory containing your project file, and Use the command dotnet add package SqlBuilder to install the package.
- Using Visual Studio, Go to Tools > NuGet Package Manager > Package Manager Console, under Browse search for SqlBuilder, select the latest version and install.
Initial Setup
Usage
Below are the requirements to run the following sample code.
- Access to northwind database.
- Appropriate connection to northwind database
- Create a console application [say, DemoApplication with .NET 8 Core]
- Install Microsoft.Data.SqlClient package in the console application
- Install Dapper package in the console application
- Install SqlBuilder_QueryInCode package in the console application
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(); ListcustomCustomers = 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}."; } } }