C#: SQL Query Builder Class C#: SQL Query Builder Class mysql mysql

C#: SQL Query Builder Class


Since Google leads me to this page,I would suggest SqlKata, a simple but powerful SqlQuery Builder, that supports nested where conditions, subqueries and joins.

Currently it supports SqlServer, MySql and PostgreSql

var query = new Query("Users")     .LeftJoin("Countries", "Users.CountryId", "Countries.Id")     .Where("Status", "blocked")     .OrWhereIn("Id", new [] {10, 11, 12})     .OrWhere("LastLogin", ">", DateTime.UtcNow.AddMonths(-5));

Note: I am the owner of it

Difference between different compilers output
MySql: https://sqlkata.com/playground/mysql?code=var%20query%20=%20new%20Query(%22Posts%22).Limit(10).Offset(20)%3B

SqlServer: https://sqlkata.com/playground/sqlserver?code=var%20query%20=%20new%20Query(%22Posts%22).Limit(10).Offset(20)%3B

Oracle: https://sqlkata.com/playground/oracle?code=var%20query%20=%20new%20Query(%22Posts%22).Limit(10).Offset(20)%3B


You could probably use the framework class CommandBuilder. Check out:

http://msdn.microsoft.com/en-us/library/tf579hcz.aspx


If you are using .NET 4 and don't mind using dynamics you can use Massive, created by Rob Conery, this single file Database requires no dlls, just drop the Massive.cs file and you ready to go.

You can use the Massive to build queries like this.

//grab all the productsvar products = table.All();//Orvar productsFour = table.All(columns: "ProductName as Name", where: "WHERE categoryID=@0",args: 4);

You can also run ad-hoc queries as needed:

var result = tbl.Query("SELECT * FROM Categories");