SharpOrm 1.2.80

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package SharpOrm --version 1.2.80
NuGet\Install-Package SharpOrm -Version 1.2.80
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="SharpOrm" Version="1.2.80" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SharpOrm --version 1.2.80
#r "nuget: SharpOrm, 1.2.80"
#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 SharpOrm as a Cake Addin
#addin nuget:?package=SharpOrm&version=1.2.80

// Install SharpOrm as a Cake Tool
#tool nuget:?package=SharpOrm&version=1.2.80

Sharp Orm is a BETA library that simplifies the creation of database query. Tested with Mysql and Microsoft Sql Server, but also has sqlite compatibility.

There are two ways to work with the library, defining a global configuration or passing the configuration in each query.

In both cases, it is necessary to define a class that implements the interface "SharpOrm.Builder.IQueryConfig" in order to work with the database, this step is necessary for the library to know how to transform objects into a query.

Using global configuration

To use a global configuration you need to create a new instance of ConnectionCreator, you can create your own class to implement custom rules but in most cases you can use SingleConnection class or MultipleConnectionCreator.

  • SingleConnection: Uses only one connection to execute all the operations in the database.
  • MultipleConnectionCreator: Uses one connection to perform each operation on the database.

Configuring the global configuration

using SharpOrm.Builder;
using SharpOrm.Connection;

//For Mysql
ConnectionCreator.Default = new SingleConnectionCreator<System.Data.SQLite.SQLiteConnection>(new MysqlQueryConfig(false), connectionString);
//For Sqlite
ConnectionCreator.Default = new SingleConnectionCreator<MySql.Data.MySqlClient.MySqlConnection>(new MysqlQueryConfig(false), connectionString);
//For Microsoft Sql Server
ConnectionCreator.Default = new SingleConnectionCreator(new SqlServerQueryConfig(false), connectionString);

Using global configuration

using SharpOrm;

//Class responsible for performing the request in the database.
using(Query query = new Query("Users"))
{
    //Filter that must be used to retrieve the rows from the database.
    query.Where("active", "=", 1);
    Row[] users = query.ReadRows();
}

Using query configuration

using SharpOrm;
using SharpOrm.Builder;

var connection = //You connection instance here.
//For mysql
IQueryConfig config = new MysqlQueryConfig();
//For Microsoft Sql Server
IQueryConfig config = new SqlServerQueryConfig();

using(Query query = new Query(connection, config, "Users"))
{
    query.Where("active", "=", 1);
    Row[] users = query.ReadRows();
}

It is possible to create a Query for a specific model

User class

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

[Table("Users")]//It is recommended to use this attribute, but it is not required.
public class User
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    [Column("record_created")]
    public DateTime CreatedAt { get; set; }
}

Sample code

using SharpOrm;
using SharpOrm.Builder;

using(Query<User> query = new Query<User>())
{
    User user = query.Find(1);//Retrieving a user by id (to use this function, it is necessary that some property has the Key attribute)
    //OR
    query.Where("Id", 1);//Signals to the query that only users with id 1 should be selected (WHERE `Id` = 1).
    query.FirstOrDefault();//Returns the first value that meets the specifications, or returns null if it does not.
}

Inserting values

It is possible to use a C# object with the same structure as the database or use Cell to insert the values.

Using C# objects

using SharpOrm;
using SharpOrm.Builder;

using(Query<User> query = new Query<User>())
{
    //Single insert
    query.Insert(new User
    {
        Id = 1,
        Name = "My name",
        Nick = "My nick",
        CreatedAt = System.DateTime.Now
    });

    //Multiple insert
    query.BulkInsert(
        new User{ ... },
        new User{ ... },
        new User{ ... }
    );
}

Using Cell and Row (for multiple insert)

using(Query query = new Query("Users"))
{
    //Single insert
    query.Insert(new Cell("Id", 1), new Cell("Name", "My name"), new Cell("Nick", "My nick"), new Cell("CreatedAt", System.DateTime.Now));

    //Multiple insert
    query.BulkInsert(
        new Row(new Cell("Id", 1), new Cell("Name", "My name"), new Cell("Nick", "My nick"), new Cell("CreatedAt", System.DateTime.Now)),
        new Row(...),
        new Row(...)
    );
}
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 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 net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.2.80 90 4/6/2024
1.2.70 77 3/18/2024
1.2.60 84 2/17/2024
1.2.50 147 12/30/2023
1.2.40 154 11/28/2023
1.2.31 120 11/8/2023
1.2.26 164 9/1/2023
1.2.25 156 7/29/2023
1.2.11 141 7/14/2023
1.2.0 144 6/18/2023
1.1.78 116 5/18/2023
1.1.68 130 5/15/2023
1.1.67 143 5/3/2023

Correction of connection management, cancellation token management, and improvements in Query and Grammar.