Welcome to Fluid-SQL
This library allows .NET developers to build database-independent SQL queries in C# (or other CLR language) using fluid coding style.
For example, you can write code like this:
var statement =
Sql.Select.From("mytable", "n")
.Output("n.*")
.Where(Sql.Name("n", "id").LessOrEqual(Sql.Scalar(3)));
Variable statement
contains AST for your query. Now you can simply use one of the language providers to create to get either a corresponding SQL:
var provider = new SqlServerProvider();
Console.WriteLine(provider.GenerateStatement(statement));
output:
SELECT [n].* FROM [mytable] AS [n] WHERE [n].[id] <= 3;
or Command object
var provider = new SqlServerProvider();
using (var command = provider.GetCommand(statement, connectionString))
{
using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
do
{
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Trace.Write(reader.GetValue(i));
Trace.Write("\t");
}
Trace.Write("\r\n");
}
} while (reader.NextResult());
}
}
By switching to Sqlite provider, you can use the same code to target Sqlite database. Support for MySql and PostgreSQL is coming soon.
Main features
Using Fluid-SQL you can not only query data, but also
- 'CREATE/ALTER/DELETE' tables and views
- Create `IF BEGIN-END ELSE BEGIN_END' blocks
- 'INSERT/MERGE/DELETE' data
- Hide certain language differences, like date manipulation functions
- and many more
Setup
Fluid-Sql is available on Nuget: https://www.nuget.org/packages/TTRider.FluidSql.dll/