DatabaseWrapper.Core 6.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package DatabaseWrapper.Core --version 6.0.2                
NuGet\Install-Package DatabaseWrapper.Core -Version 6.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DatabaseWrapper.Core" Version="6.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DatabaseWrapper.Core --version 6.0.2                
#r "nuget: DatabaseWrapper.Core, 6.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DatabaseWrapper.Core as a Cake Addin
#addin nuget:?package=DatabaseWrapper.Core&version=6.0.2

// Install DatabaseWrapper.Core as a Cake Tool
#tool nuget:?package=DatabaseWrapper.Core&version=6.0.2                

DatabaseWrapper

Library Version Downloads
DatabaseWrapper (all supported database types) NuGet Version NuGet
DatabaseWrapper.Mysql NuGet Version NuGet
DatabaseWrapper.Postgresql NuGet Version NuGet
DatabaseWrapper.Sqlite NuGet Version NuGet
DatabaseWrapper.SqlServer NuGet Version NuGet
DatabaseWrapper.Core NuGet Version NuGet
ExpressionTree NuGet Version NuGet

DatabaseWrapper is the EASIEST and FASTEST way to get a data-driven application up and running using SQL Server, MySQL, PostgreSQL, or Sqlite.

For a sample app exercising this library, refer to the numerous Test projects contained within the solution.

Core features:

  • Dynamic query building
  • Hierarchical Boolean logic using Expression objects
  • Support for SQL server native vs Windows authentication
  • Support for SELECT, INSERT, UPDATE, DELETE, TRUNCATE, CREATE, DROP or raw queries
  • Programmatic table creation and removal (drop)
  • Built-in sanitization
  • Support for .NET Standard, .NET Core, and .NET Framework
  • Support for SQL Server, Sqlite, PostgreSQL, MySQL, MariaDB, both on-premises and in the cloud

New in v6.0.x

  • Minor breaking changes (API name changes)
  • Asynchronous APIs
  • Updated dependencies
  • Bugfixes

Special Thanks

Special thanks to those who have helped contribute or otherwise improve the library!

@shawty @constantinje @thedarklort @l-404-l @igrgurina @Vaelek @treyhay31 @daoye @twobytescy @rinkusahu1

A Note on Sanitization

Use of parameterized queries vs building queries dynamically is a sensitive subject. Proponents of parameterized queries have data on their side - that parameterization does the right thing to prevent SQL injection and other issues. I do not disagree with them. However, it is worth noting that with proper care, you CAN build systems that allow you to dynamically build queries, and you SHOULD do so as long as you build in the appropriate safeguards.

If you find an injection attack that will defeat the sanitization layer built into this project, please let me know!

Simple Example

Refer to the test project for a more complete example with sample table setup scripts.

using DatabaseWrapper;
using DatabaseWrapper.Core;
using ExpressionTree;

DatabaseClient client = null;

// Sqlite
client = new DatabaseClient("[databasefilename]");

// SQL Server, MySQL, or PostgreSQL
client = new DatabaseClient(DbTypes.SqlServer,  "[hostname]", [port], "[user]", "[password]", "[databasename]");
client = new DatabaseClient(DbTypes.Mysql,      "[hostname]", [port], "[user]", "[password]", "[databasename]");
client = new DatabaseClient(DbTypes.Postgresql, "[hostname]", [port], "[user]", "[password]", "[databasename]");

// SQL Express
client = new DatabaseClient(DbTypes.SqlServer,  "[hostname]", [port], "[user]", "[password]", "[instance]", "[databasename]");

// some variables we'll be using
Dictionary<string, object> d;
Expr e;
List<string> fields;
DataTable result;

// add a record
d = new Dictionary<string, object>();
d.Add("firstName", "Joel");
d.Add("lastName", "Christner");
d.Add("notes", "Author");
result = client.Insert("person", d);

// update a record
d = new Dictionary<string, object>();
d.Add("notes", "The author :)");
e = new Expr("firstName", OperatorEnum.Equals, "Joel"); 
client.Update("person", d, e);

// retrieve 10 records
fields = new List<string> { "firstName", "lastName" }; // leave null for *
e = new Expr("lastName", OperatorEnum.Equals, "Christner"); 
ResultOrder[] order = new ResultOrder[1];
order = new ResultOrder("firstName", OrderDirectionEnum.Ascending)
result = client.Select("person", 0, 10, fields, e, order);

// delete a record
e = new Expr("firstName", Operators.Equals, "Joel"); 
client.Delete("person", e);

// execute a raw query
result = client.Query("SELECT customer_id FROM customer WHERE customer_id > 10");

Sample Compound Expression

Expressions, i.e. the Expr class from ExpressionTree, can be nested in either the Left or Right properties. Conversion from Expr to a WHERE clause uses recursion, so you have a good degree of flexibility in building your expressions in terms of depth.

Expr e = new Expr {
	Left = new Expr("age", OperatorEnum.GreaterThan, 30),
	Operator = Operators.And,
	Right = new Expr("height", OperatorEnum.LessThan, 74)
};

Select with Pagination

Use IndexStart, MaxResults, and ResultOrder[] to retrieve paginated results. The query will retrieve maxResults records starting at row number indexStart using an ordering based on orderByClause. See the example in the DatabaseWrapperTest project.

IMPORTANT: When doing pagination with SQL Server, you MUST specify an ResultOrder[].

ResultOrder[] order = new ResultOrder[1];
order = new ResultOrder("firstName", OrderDirectionEnum.Ascending);
DataTable result = client.Select("person", 5, 10, null, e, order);

Need a Timestamp?

We added a simple static method for this which you can use when building expressions (or elsewhere). An object method exists as well.

string ts = client.Timestamp(DateTime.Now);
// 08/23/2016 05:34:32.4349034 PM

string tso = client.Timestamp(DateTime.Now);
// 2016-08-23 17:34:32.446913 

Other Notes

General

When using database-specific classes DatabaseWrapper.Mysql, DatabaseWrapper.Postgresql, DatabaseWrapper.SqlServer, or DatabaseWrapper.Sqlite, the constructor is simplified from what is shown above.

For Sqlite:

DatabaseClient client = new DatabaseClient("[databasefilename]");

For SQL Server, MySQL, or PostgreSQL:

DatabaseClient client = new DatabaseClient(DbTypes.SqlServer,  "[hostname]", [port], "[user]", "[password]", "[databasename]");
DatabaseClient client = new DatabaseClient(DbTypes.Mysql,      "[hostname]", [port], "[user]", "[password]", "[databasename]");
DatabaseClient client = new DatabaseClient(DbTypes.Postgresql, "[hostname]", [port], "[user]", "[password]", "[databasename]");

For SQL Server Express:

DatabaseClient client = new DatabaseClient(DbTypes.SqlServer, "[hostname]", [port], "[user]", "[password]", "[instance]", "[databasename]");

MySQL

  • MySQL does not like to return updated rows. I thought about making the UPDATE clause require that you supply the ID field and the ID value so that I could retrieve it after the fact, but that approach is just too limiting.

MariaDB

  • Use the MySQL constructor. MySQL constraints remain.

PostgreSQL

  • Cleansing of strings in PostgreSQL uses the dollar-quote style. Fieldnames are always encapsulated in double-quotes for PostgreSQL.

SQL Server

  • Pagination where IndexStart and MaxResults are supplied demands use of ResultOrder[].

Sqlite

  • Sqlite may not work out of the box with .NET Framework. In order to use Sqlite with .NET Framework, you'll need to manually copy the runtimes folder into your project output directory. This directory is automatically created when building for .NET Core. To get this folder, build the Test.Sqlite project and navigate to the bin/debug/netcoreapp* directory. Then copy the runtimes folder into the project output directory of your .NET Framework application.

Version history

Refer to CHANGELOG.md.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on DatabaseWrapper.Core:

Package Downloads
DatabaseWrapper.Sqlite

Simple database wrapper for Sqlite written in C# supporting dynamic query building and nesting using expressions. Refer to other DatabaseWrapper packages for support for SQL Server, MySQL, and PostgreSQL.

DatabaseWrapper.Mysql

Simple database wrapper for MySQL written in C# supporting dynamic query building and nesting using expressions. Refer to other DatabaseWrapper packages for support for SQL Server, Sqlite, and PostgreSQL.

DatabaseWrapper.SqlServer

Simple database wrapper for SQL Server written in C# supporting dynamic query building and nesting using expressions. Refer to other DatabaseWrapper packages for support for MySQL, Sqlite, and PostgreSQL.

DatabaseWrapper.Postgresql

Simple database wrapper for Postgresql written in C# supporting dynamic query building and nesting using expressions. Refer to other DatabaseWrapper packages for support for SQL Server, Sqlite, and MySQL.

WatsonORM.Core

Do not install this package directly. Core classes and static methods supporting WatsonORM packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.1.0 542 9/19/2024
6.0.6 5,704 10/4/2023
6.0.5 5,835 8/29/2023
6.0.4 6,470 7/11/2023
6.0.3 198 7/11/2023
6.0.2 1,380 7/11/2023
6.0.1 172 7/11/2023
6.0.0 181 7/11/2023
5.0.3 440 2/2/2023
5.0.2 5,427 2/2/2023
5.0.1 1,196 2/2/2023
5.0.0 6,798 10/25/2022
4.1.5 11,841 8/29/2022
4.1.3 6,663 6/21/2022
4.1.1 7,405 5/27/2022
4.1.0 444 5/27/2022
4.0.0.1 7,177 12/31/2021
4.0.0 1,939 12/31/2021
3.3.1.8 2,350 12/22/2021
3.3.1.7 5,522 11/19/2021
3.3.1.6 785 11/19/2021
3.3.1.5 5,697 11/12/2021
3.3.1.3 5,851 6/7/2021
3.3.1.2 1,767 4/27/2021
3.3.1.1 11,023 4/14/2021
3.3.1 391 4/14/2021
3.2.2.10 18,256 11/15/2020
3.2.0.6 48,252 10/15/2020
3.2.0.5 13,702 9/18/2020
3.1.0.2 21,327 9/8/2020
3.1.0.1 39,349 6/11/2020
3.1.0 50,876 6/11/2020
3.0.1.3 1,700 6/11/2020
3.0.1.1 8,042 6/4/2020
3.0.1 7,343 6/3/2020
3.0.0.2 15,185 5/20/2020
3.0.0.1 3,008 5/20/2020
3.0.0 539 5/20/2020

Bugfixes, retargeting to include .NET Framework 4.8 and .NET 7.0.