QueryDB is a flexible database query framework designed to simplify querying and executing transactions across multiple database systems.
The package is available and can be downloaded using nuget.org package manager.
- Package Name - QueryDB.
Built on .NET Standard 2.0 - ( Supported Versions )
- Retrieve data from the database.
- Execute scalar queries (returning a single value).
- Execute non-query database commands (e.g.
INSERT
,UPDATE
,DELETE
). - Execute transactions while maintaining atomicity.
- Support for Synchronous and Asynchronous operations.
-
Setup
DBContext
with the database of your choice :var dbContext = new DBContext(DB.<Database Type>, <Connection String>);
-
Execute
DBContext
commands :Execute DBContext Commands Synchronously
var result = dbContext.FetchData(<Sql Statement>);
var result = dbContext.FetchData<T>(<Sql Statement>);
var result = dbContext.ExecuteScalar(<Sql Statement>);
var result = dbContext.ExecuteScalar<T>(<Sql Statement>);
var result = dbContext.ExecuteCommand(<Sql Statement>);
var result = dbContext.ExecuteTransaction(<List of Sql Statements>);
Execute DBContext Commands Asynchronously
var result = dbContext.FetchDataAsync(<Sql Statement>);
var result = dbContext.FetchDataAsync<T>(<Sql Statement>);
var result = dbContext.ExecuteScalarAsync(<Sql Statement>);
var result = dbContext.ExecuteScalarAsync<T>(<Sql Statement>);
var result = dbContext.ExecuteCommandAsync(<Sql Statement>);
var result = dbContext.ExecuteTransactionAsync(<List of Sql Statements>);
Data Retrieval
public class Orders
{
public string Agent_Code { get; set; }
public string Agent { get; set; }
public string Agent_Name { get; set; }
public string Agent_Location { get; set; }
public string Cust_Code { get; set; }
public string Customer { get; set; }
public string Cust_Name { get; set; }
public string Customer_Location { get; set; }
public decimal Ord_Num { get; set; }
public decimal Ord_Amount { get; set; }
public decimal Advance_Amount { get; set; }
public string Ord_Description { get; set; }
}
var sql = @"SELECT A.Agent_Code, A.Agent_Name, C.Cust_Code, C.Cust_Name, O.Ord_Num, O.Ord_Amount, O.Advance_Amount, O.Ord_Date, O.Ord_Description FROM Agents A INNER JOIN
Customer C ON C.Agent_Code = A.Agent_Code INNER JOIN
Orders O ON O.Cust_Code = C.Cust_Code AND O.Agent_Code = A.Agent_Code";
var data = new DBContext(DB.<Database Type>, <Connection String>).FetchData<Orders>(selectSql);
var agent = data.FirstOrDefault(X => X.Agent_Name == "Foo");
Transaction
// Create, Insert & Update
var statements = new List<string>
{
"CREATE TABLE Employee (EmployeeID INT PRIMARY KEY, FirstName NVARCHAR(50), LastName NVARCHAR(50))",
"INSERT INTO Employee VALUES ('E01', 'John', 'Wick')",
"UPDATE Employee SET FirstName = 'Jack' LastName = 'Reacher' WHERE EmployeeID = 'E01'"
};
var dbContext = new DBContext(DB.MSSQL, MSSQLConnectionString);
var result = dbContext.ExecuteTransaction(statements);
---