SharpOrm 1.2.31
Prefix ReservedSee the version list below for details.
dotnet add package SharpOrm --version 1.2.31
NuGet\Install-Package SharpOrm -Version 1.2.31
<PackageReference Include="SharpOrm" Version="1.2.31" />
paket add SharpOrm --version 1.2.31
#r "nuget: SharpOrm, 1.2.31"
// Install SharpOrm as a Cake Addin #addin nuget:?package=SharpOrm&version=1.2.31 // Install SharpOrm as a Cake Tool #tool nuget:?package=SharpOrm&version=1.2.31
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 and Sqlite
ConnectionCreator.Default = new SingleConnectionCreator(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 | Versions 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. |
-
.NETFramework 4.5
- No dependencies.
-
.NETStandard 2.0
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Data.SqlClient (>= 4.8.5)
-
net6.0
- System.Data.SqlClient (>= 4.8.5)
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 |
---|---|---|
2.0.1 | 92 | 6/26/2024 |
2.0.0 | 86 | 6/6/2024 |
1.2.80 | 96 | 4/6/2024 |
1.2.70 | 81 | 3/18/2024 |
1.2.60 | 91 | 2/17/2024 |
1.2.50 | 149 | 12/30/2023 |
1.2.40 | 156 | 11/28/2023 |
1.2.31 | 124 | 11/8/2023 |
1.2.26 | 165 | 9/1/2023 |
1.2.25 | 157 | 7/29/2023 |
1.2.11 | 142 | 7/14/2023 |
1.2.0 | 145 | 6/18/2023 |
1.1.78 | 117 | 5/18/2023 |
1.1.68 | 132 | 5/15/2023 |
1.1.67 | 146 | 5/3/2023 |
Improvement in the column reading system and bug fixes.