FluentSqlBuilder 1.0.0-alpha
dotnet add package FluentSqlBuilder --version 1.0.0-alpha
NuGet\Install-Package FluentSqlBuilder -Version 1.0.0-alpha
<PackageReference Include="FluentSqlBuilder" Version="1.0.0-alpha" />
paket add FluentSqlBuilder --version 1.0.0-alpha
#r "nuget: FluentSqlBuilder, 1.0.0-alpha"
// Install FluentSqlBuilder as a Cake Addin #addin nuget:?package=FluentSqlBuilder&version=1.0.0-alpha&prerelease // Install FluentSqlBuilder as a Cake Tool #tool nuget:?package=FluentSqlBuilder&version=1.0.0-alpha&prerelease
Sql Builder
Welcome to the readme of Sql Builder. This is a library that allows developers to build a Sql Command or Sql Statement with Parameters using a strongly typed system similiar to LINQ without the hassle of creating/maintaining a db context.
Example:
All values supplied will be transformed to parameters. Therefore you won't have to worry about SQL Injection.
using (var connection = new SqlConnection())
{
var command = connection.Build()
.From<Person>()
.Join<Work>((person,work) => person.WorkId == work.WorkId)
.Join<Person>((person, work,person2)=> person.Id == person2.Id)
.Where((person, work, person2) => person2.Age.In(new int[] { 1, 2 }))
.Select((person, work, person2) => new { person.Id, person.Name })
.ToSqlCommand();
}
SELECT [Person].[Id], [Person].[Name] FROM [Person] JOIN [Work] AS [Work] ON [Person].[WorkId] = [Work].[WorkId] JOIN [Person] AS [Person1] ON [Person].[Id] = [Person1].[Id] WHERE [Person1].[Age] IN (@P1,@P2)
Currently it only supports SQL Server. All the functions can be concatenated with eachother to freely buld your own query.
You can install Sql Builder through nuget.
How to use Sql Builder?
Building a query always starts with the From<T>() function and ends with the Select() function similiar to LINQ.
Basic Select query.
using (var connection = new SqlConnection())
{
var command = connection.Build()
.From<Person>()
.Select(person => new { person.Id })
.ToSqlCommand();
}
SELECT [Person].[Id] FROM [Person]
You can add more columns to the select function by simply expanding the anonymous object within the Select function.
using (var connection = new SqlConnection())
{
var command = connection.Build()
.From<Person>()
.Select(person => new { person.Id, person.Name })
.ToSqlCommand();
}
SELECT [Person].[Id], [Person].[Name] FROM [Person]
Adding a Where Clause.
Parameters used within the where clause will automatically be added to the command with its appropriate value to prevent SQL Injection.
using (var connection = new SqlConnection())
{
var command = connection.Build()
.From<Person>()
.Where((person) => person.Salary > 5000)
.Select(person => new { person.Id, person.Name })
.ToSqlCommand();
}
SELECT [Person].[Id], [Person].[Name] FROM [Person] WHERE [Person].[Salary] > @P1
There's also support for In, Not In, Like, Not Like, Between, Not Between
using (var connection = new SqlConnection())
{
IEnumerable<int> ages = new int[] { 1, 2 };
var command = connection.Build()
.From<Person>()
.Where((person) => person.Age.In(ages))
.Select(person => new { person.Id, person.Name })
.ToSqlCommand();
}
SELECT [Person].[Id], [Person].[Name] FROM [Person] WHERE [Person].[Age] IN (@P1,@P2)
using (var connection = new SqlConnection())
{
var command = connection.Build()
.From<Person>()
.Where((person) => person.Name.Like("base%"))
.Select(person => new { person.Id, person.Name })
.ToSqlCommand();
}
SELECT [Person].[Id], [Person].[Name] FROM [Person] WHERE [Person].[Age] LIKE @P1
using (var connection = new SqlConnection())
{
var command = connection.Build()
.From<Person>()
.Where((person) => person.Age.Between(1,2))
.Select(person => new { person.Id, person.Name })
.ToSqlCommand();
}
SELECT [Person].[Id], [Person].[Name] FROM [Person] WHERE [Person].[Age] BETWEEN @P1 AND @P2
Joining other tables
using (var connection = new SqlConnection())
{
var command = connection.Build()
.From<Person>()
.Join<Work>((person,work) => person.WorkId == work.WorkId)
.Select((person, work) => new { person.Id, person.Name, work.Address })
.ToSqlCommand();
}
SELECT [Person].[Id], [Person].[Name], [Work].[Address] FROM [Person] JOIN [Work] AS [Work] ON [Person].[WorkId] = [Work].[WorkId]
There's also support for self-joining
using (var connection = new SqlConnection())
{
var command = connection.Build()
.From<Person>()
.Join<Person>((person, person2) => person.WorkId == person2.WorkId)
.Select((person, person2) => new { person.Id, person2.Name })
.ToSqlCommand();
}
SELECT [Person].[Id], [Person1].[Name] FROM [Person] JOIN [Person] AS [Person1] ON [Person].[WorkId] = [Person1].[WorkId]
You can join multiple tables aswell by using the Join function over and over.
using (var connection = new SqlConnection())
{
var command = connection.Build()
.From<Person>()
.Join<Person>((person, person2) => person.WorkId == person2.WorkId)
.Join<Person>((person, person2,person3) => person.Name == person3.Name)
.Select((person, person2, person3) => new { person.Id, person2.Name , person3.Age })
.ToSqlCommand();
}
SELECT [Person].[Id], [Person1].[Name], [Person2].[Age] FROM [Person] JOIN [Person] AS [Person1] ON [Person].[WorkId] = [Person1].[WorkId] JOIN [Person] AS [Person2] ON [Person].[Name] = [Person2].[Name]
Adding a Group By Clause
using (var connection = new SqlConnection())
{
var command = connection.Build()
.From<Person>()
.GroupBy((person) => new { person.Age })
.Select(person => new { person.Id, person.Name })
.ToSqlCommand();
}
SELECT [Person].[Id], [Person].[Name] FROM [Person] GROUP BY [Person].[Age]
There's also support to do Group By on multiple columns.
using (var connection = new SqlConnection())
{
var command = connection.Build()
.From<Person>()
.GroupBy((person) => new { person.Age , person.Name })
.Select(person => new { person.Id, person.Name })
.ToSqlCommand();
}
SELECT [Person].[Id], [Person].[Name] FROM [Person] GROUP BY [Person].[Age], [Person].[Name]
Adding an Order By Clause
using (var connection = new SqlConnection())
{
var command = connection.Build()
.From<Person>()
.GroupBy((person) => new { person.Age , person.Name })
.Select(person => new { person.Id, person.Name })
.ToSqlCommand();
}
SELECT [Person].[Id], [Person].[Name] FROM [Person] ORDER BY [Person].[Age]
There's also support to do Order By on multiple columns.
using (var connection = new SqlConnection())
{
var command = connection.Build()
.From<Person>()
.OrderBy((person) => new { person.Age , person.Salary })
.Select(person => new { person.Id, person.Name })
.ToSqlCommand();
}
SELECT [Person].[Id], [Person].[Name] FROM [Person] ORDER BY [Person].[Age], [Person].[Salary]
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Data.SqlClient (>= 4.8.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0-alpha | 334 | 6/27/2020 |
Alpha release as a proof of concept.