If your project needs to fire queries to a relational database often at different places of your code. This lightweight library will be helpfull.
Express.Data allows playing with database queries easy by making use of Dapper, DapperContrib and DTO classes. Examples are provided below. This library is a wrapper around the popular Dapper micro ORM and it's extension Dapper.Contrib. It works with any relational database and runs over ADO.NET and DapperMicroORM
The library exposes easy to use API for all kind of database CRUD operations.
The library is available free on NuGet https://www.nuget.org/packages/Twileloop.ExpressData
Install-Package Twileloop.ExpressData -Version 1.0.0
Version Information
Version | Change log |
---|---|
v1.1 | Basic CRUD Operations support |
This repo maintains 2 projects. The main library and a demo project to implement it
Before running the demo program
- Create a table named tblUsers
- Create 3 columns Id, Fname and Lname
- Set the connection string to database server inside Main()
C# POCO Model building. The model should be decorated with Dapper.Contrib attributes for proper working. Here is an example
using Dapper.Contrib.Extensions;
namespace Demo
{
[Table("tblUser")]
public class tblUser
{
[Key]
public int Id { get; set; }
public string FName { get; set; }
public string LName { get; set; }
[Computed]
public string Fullname { get; set; }
}
}
Insert a model
var record = new tblUser {
Id = 1,
FName = "Sangeeth",
LName = "Nandakumar"
};
var primaryKey = SqlHelper.Insert < tblUser > (record, _connectionString);
Update a model
record = new tblUser {
Id = 1,
FName = "Navaneeth",
LName = "Nandakumar"
};
var isUpdated = SqlHelper.Update < tblUser > (record, _connectionString);
Run a query
var sql1 = $ "SELECT * FROM tblUser WHERE Id=1";
var result = SqlHelper.Query < tblUser > (sql1, _connectionString).FirstOrDefault();
Run a safe query (Parameterised query)
var sql2 = $ "SELECT * FROM tblUser WHERE Id=@id AND Fname=@fname";
result = SqlHelper.QuerySafe < tblUser > (sql2, new {
id = 1,
fname = "Navaneeth"
},
_connectionString);
Run a stored procedure
var procedureName = $ "spGetUsers";
result = SqlHelper.Execute < tblUser > (procedureName, new {
id = 1
},
_connectionString).FirstOrDefault();