HIC.FAnsiSql 3.2.2

dotnet add package HIC.FAnsiSql --version 3.2.2
NuGet\Install-Package HIC.FAnsiSql -Version 3.2.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="HIC.FAnsiSql" Version="3.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HIC.FAnsiSql --version 3.2.2
#r "nuget: HIC.FAnsiSql, 3.2.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 HIC.FAnsiSql as a Cake Addin
#addin nuget:?package=HIC.FAnsiSql&version=3.2.2

// Install HIC.FAnsiSql as a Cake Tool
#tool nuget:?package=HIC.FAnsiSql&version=3.2.2

FAnsiSql

Build and test CodeQL NuGet Badge

<p align="right"> <a href="https://www.publicdomainpictures.net/en/view-image.php?image=184699&picture=a-laugh-every-day-126"> <img src="FansiHammerSmall.png" align="right"/> </a> </p>

Ever had difficulty getting a DataTable into a database? Maybe the dates are going in as strings or some clever dude put spaces in the middle of column names? FAnsiSql has you covered:

//Some data we want to load
var dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Date of Birth");
dt.Rows.Add("Frank \"The Boss\" Spagetti","1920-01-01");
dt.Rows.Add("Pete Mudarillo","22-May-1910");

//Load the DBMS implementation(s) you need
ImplementationManager.Load<MicrosoftSQLImplementation>();

//Get management object for the database
var server = new DiscoveredServer(
    @"server=localhost\sqlexpress;Trusted_Connection=True;",
     DatabaseType.MicrosoftSQLServer);

var database = server.ExpectDatabase("test");
var table = database.ExpectTable("MyTable");
            
//Throw out whatever was there before
if(table.Exists())
    table.Drop();

//Create the table
database.CreateTable("MyTable",dt);
            
//Database types are compatible with all the data
Assert.AreEqual("datetime2",
    table.DiscoverColumn("Date of Birth").DataType.SQLType);

Assert.AreEqual("varchar(25)",
    table.DiscoverColumn("Name").DataType.SQLType);

//And the (string) data is now properly typed and sat in our DBMS
Assert.AreEqual(2,table.GetRowCount());    
Assert.AreEqual(new DateTime(1920,1,1),
    table.GetDataTable().Rows[0][1]);

Assert.AreEqual(new DateTime(1910,5,22),
    table.GetDataTable().Rows[1][1]);

FAnsi Sql! it's like a budget version of SMO (that works cross platform - Sql Server, MySql, Postgres and Oracle). It supports:

  • Table Creation
  • Assigning types to untyped (string) data
  • Bulk Insert
  • DDL operations (Create database, drop database etc)
  • Discovery (Does table exist?, what columns are in table? etc)
  • Query writting assistance (e.g. TOP X)

It is not an ORM, it deals only in value type data (Strings, System.DataTable, Value Types, SQL etc).

Install

FAnsi Sql is a nuget package. You can install it using either using the package manager:

PM> Install-Package HIC.FansiSql

Or .NET CLI Console:

> dotnet add package HIC.FansiSql

Feature Completeness

Most features are implemented across all 4 DBMS, you can find a breakdown of progress here:

Implementations are defined in separate assemblies (e.g. FAnsi.Implementations.MicrosoftSQL.dll) to allow for future expansion. Each implementation uses it's own backing library (e.g. ODP.net for Oracle). Implementations are loaded using Managed Extensibility Framework.

Why is it useful?

FAnsiSql is a database management/ETL library that allows you to perform common SQL operations without having to know which Database Management System (DBMS) you are targetting (e.g. Sql Server, My Sql, Oracle).

Consider writing an SQL create table command:

CREATE TABLE table_name (
	column1 datatype,
	column2 datatype,
	column3 datatype,
	....
);

If we wanted to target Microsoft Sql Server we might write something like:

CREATE TABLE [FAnsiTests].[dbo].[MyTable]
(
	Name varchar(10) NULL,
	DateOfBirth datetime2 NULL,
);

The same code on MySql would be:

CREATE TABLE `FAnsiTests`.`MyTable`
(
	`Name` varchar(5) NULL ,
	`DateOfBirth` datetime NULL 
);

We have to change the table qualifier, we don't specify schema (dbo) and even the data types are different. The more advanced the feature, the more disparate the varied the implementations are (e.g. TOP X, UPDATE from JOIN etc).

The goal of FAnsiSql is to abstract away cross DBMS differences and streamline common tasks while still allowing you to harness the power of executing raw SQL commands.

Example

Imagine we have a System.DataTable in memory and we want to store it in a database with an appropriate schema.

//Create some test data
DataTable dt = new DataTable();

dt.Columns.Add("Name");
dt.Columns.Add("DateOfBirth");

dt.Rows.Add("Frank","2001-01-01");
dt.Rows.Add("Dave", "2001-01-01");

//Load implementation assemblies that are relevant to your application  (do this once on startup)
ImplementationManager.Load(
	typeof(FAnsi.Implementations.MicrosoftSQL.MicrosoftSQLImplementation).Assembly,
	typeof(FAnsi.Implementations.MySql.MySqlImplementation).Assembly);

//Create a server object
var server = new DiscoveredServer(@"server=localhost\sqlexpress;Trusted_Connection=True;", DatabaseType.MicrosoftSQLServer);

//Find the database
var database = server.ExpectDatabase("FAnsiTests");

//Or create it
if(!database.Exists())
	database.Create();

//Create a table that can store the data in dt
var table = database.CreateTable("MyTable", dt);

//Table has 2 rows in it
Console.WriteLine("Table {0} has {1} rows" ,table.GetFullyQualifiedName(), table.GetRowCount());
Console.WriteLine("Column Name is of type {0}", table.DiscoverColumn("Name").DataType.SQLType);
Console.WriteLine("Column DateOfBirth is of type {0}", table.DiscoverColumn("DateOfBirth").DataType.SQLType);

//Drop the table afterwards
table.Drop();

This will output the following:

Table [FAnsiTests]..[MyTable] has 2 rows
Column Name is of type varchar(5)
Column DateOfBirth is of type datetime2

We can get the same code to execute against a MySql server by changing only the connection string line:

var server = new DiscoveredServer(@"Server=localhost;Uid=root;Pwd=***;SSLMode=None", DatabaseType.MySql);

In this case we get the following output:

Table `FAnsiTests`.`MyTable` has 2 rows
Column Name is of type varchar(5)
Column DateOfBirth is of type datetime

We can still execute raw ANSI Sql against the table

using (DbConnection con = server.GetConnection())
{
    con.Open();
    using(DbCommand cmd = server.GetCommand("Select * from " + table.GetFullyQualifiedName(), con))
        using(DbDataReader r = cmd.ExecuteReader())
            while (r.Read())
                Console.WriteLine(string.Join(",", r["Name"],r["DateOfBirth"]));
}

Building

Building requires MSBuild 15 or later (or Visual Studio 2017 or later). You will also need to install the DotNetCore 2.2 SDK.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on HIC.FAnsiSql:

Package Downloads
HIC.RDMP.Plugin

Core package for plugin development

HIC.DicomTypeTranslation

Extension library for FoDicom supporting flexible relational database schemas for storing large Dicom imaging datasets. Also supports persisting Dicom tag data to MongoDb

IsIdentifiable

Library for spotting identifiable data in flat files, dicom files and relational databases (Sql Server, MySql, Oracle).

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.2.2 1,244 3/13/2024
3.2.1 480 3/11/2024
3.2.0 299 3/5/2024
3.1.1 8,361 9/6/2023
3.1.0 2,528 5/30/2023
3.0.1 15,640 10/28/2022
3.0.0 10,798 8/29/2022
2.0.5 2,249 8/23/2022
2.0.4 21,521 4/21/2022
2.0.3 11,115 2/22/2022
2.0.2 3,907 2/3/2022
2.0.1 22,084 7/27/2021
1.0.7 3,986 5/18/2021
1.0.6 14,891 9/16/2020
1.0.5 9,568 8/13/2020
1.0.4 1,304 8/10/2020
1.0.3 1,250 8/6/2020
1.0.2 4,287 7/7/2020
1.0.1 1,305 7/7/2020
0.11.1 13,880 2/27/2020
0.11.0 1,313 2/27/2020
0.10.13 7,190 11/25/2019
0.10.12 6,990 11/19/2019
0.10.11 1,371 11/18/2019
0.10.10 3,009 11/7/2019
0.10.9 1,413 11/4/2019
0.10.8 1,270 11/4/2019
0.10.7 3,061 9/30/2019
0.10.6 2,565 9/16/2019
0.10.5 1,352 9/16/2019
0.10.4 4,953 9/11/2019
0.10.3 1,392 9/10/2019
0.10.2 1,341 9/5/2019
0.10.1 1,346 9/5/2019
0.10.0 1,494 8/30/2019
0.9.8 1,371 8/26/2019
0.9.7 1,307 8/20/2019
0.9.6 1,393 8/9/2019
0.9.5 1,381 8/8/2019
0.9.4 2,937 7/29/2019
0.9.3 1,419 7/19/2019
0.9.2 3,164 7/4/2019
0.9.1.10 3,340 5/28/2019
0.9.1.9 1,398 5/28/2019
0.9.1.8 2,062 5/22/2019
0.9.1.7 1,428 5/21/2019
0.9.1.6 1,407 5/20/2019
0.9.1.5 1,384 5/17/2019
0.9.1.4 1,444 5/16/2019
0.9.1.3 2,485 5/14/2019
0.9.1.1 5,024 5/7/2019
0.9.0.10 1,386 4/12/2019
0.9.0.9 1,357 4/10/2019
0.9.0.8 1,413 4/10/2019
0.9.0.7 1,337 4/10/2019
0.9.0.6 1,404 4/10/2019
0.9.0.5 1,539 1/18/2019
0.9.0.4 1,466 1/18/2019
0.9.0.3 1,318 1/11/2019
0.9.0.2 1,275 1/11/2019
0.9.0.1 13,367 1/10/2019