Msb.DatabaseContext
4.0.0
dotnet add package Msb.DatabaseContext --version 4.0.0
NuGet\Install-Package Msb.DatabaseContext -Version 4.0.0
<PackageReference Include="Msb.DatabaseContext" Version="4.0.0" />
paket add Msb.DatabaseContext --version 4.0.0
#r "nuget: Msb.DatabaseContext, 4.0.0"
// Install Msb.DatabaseContext as a Cake Addin #addin nuget:?package=Msb.DatabaseContext&version=4.0.0 // Install Msb.DatabaseContext as a Cake Tool #tool nuget:?package=Msb.DatabaseContext&version=4.0.0
DatabaseContext Utility Documentation
This document provides detailed documentation on the methods available in the DatabaseContext
class, which facilitates database operations such as executing queries and retrieving data. It is designed to simplify the process of interacting with a database through DbConnection
.
Getting Started
Connection String Example
Ensure you have a valid connection string to connect to the database. Below are examples for different database providers:
SQL Server:
string connectionStringsMsSql = @"Data Source=MADHUSUDAN\SQLEXPRESS;Initial Catalog=Nootbook;uid=MADHUSUDAN\Madhusudan;persistsecurityinfo=True;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionStringsMsSql))
{
var data = connection.Execute<YourClass>("SP_Users_Details_By_Id", new { Id = 1 });
}
MySQL:
string connectionStringsMySql = "Server=localhost;Database=notebook;Uid=root;Pwd=password;";
using (MySqlConnection connection = new MySqlConnection(connectionStringsMySql))
{
var data = connection.Execute<YourClass>("SP_Users_Details_By_Id", new { Id = 1 });
}
PostgreSQL:
string connectionStringsPostgres = "Host=localhost;Database=notebook;Username=postgres;Password=password;";
using (NpgsqlConnection connection = new NpgsqlConnection(connectionStringsPostgres))
{
var data = connection.RunTableQuery<YourClass>("SP_Users_Details_By_Id", new { Id = 1 });
}
Methods Overview
The DatabaseContext
class provides the following types of methods:
Execute
Methods: Execute stored procedures.Query
Methods: Execute raw SQL queries.RunTableQuery
Methods: Execute Table Function(PostgreSQL) SQL queries.ExecuteScalar
Methods: Retrieve single scalar values.- Asynchronous Variants: All of the above methods also have asynchronous versions (suffixed with
Async
).
Each type of method has overloads to support:
- No parameters.
- Anonymous object parameters.
- Generic parameters (
P
) for typed arguments.
Method Documentation
1. Execute
Executes a stored procedure and returns a list of results.
- Usage Example:
var result = connection.Execute<MyClass>("SP_Name", new { Param1 = value1 });
- Overloads:
Execute<T>(DbConnection connection, string query) where T : class, new()
Execute<T>(DbConnection connection, string query, object parameters) where T : class, new()
Execute<T, P>(DbConnection connection, string query, P parameters) where T : class, new()
2. Query
Executes a raw SQL query and returns a list of results.
- Usage Example:
var result = connection.Query<MyClass>("SELECT * FROM Table WHERE Id = @Id", new { Id = 1 });
- Overloads:
Query<T>(DbConnection connection, string query) where T : class, new()
Query<T>(DbConnection connection, string query, object parameters) where T : class, new()
Query<T, P>(DbConnection connection, string query, P parameters) where T : class, new()
3. RunTableQuery
Executes a SQL query and binds parameters, returning a list of results.
- Usage Example:
var result = connection.RunTableQuery<MyClass>("Your Table function Name", new { Name = "John" });
- Overloads:
RunTableQuery<T>(DbConnection connection, string query) where T : class, new()
RunTableQuery<T>(DbConnection connection, string query, object parameters) where T : class, new()
RunTableQuery<T, P>(DbConnection connection, string query, P parameters) where T : class, new()
4. ExecuteScalar
Executes a stored procedure or query and returns a single scalar value.
- Usage Example:
var result = connection.ExecuteScalar<int>("SP_Count_Records", new { TableName = "Users" });
- Overloads:
ExecuteScalar<T>(DbConnection connection, string query)
ExecuteScalar<T>(DbConnection connection, string query, object parameters)
ExecuteScalar<P, T>(DbConnection connection, string query, P parameters)
5. Asynchronous Methods
All the above methods have asynchronous counterparts with the Async
suffix.
- Usage Example:
var result = await connection.ExecuteAsync<MyClass>("SP_Name", new { Param1 = value1 });
- Overloads: Same as synchronous methods but with
Task<IEnumerable<T>>
as return type.
Additional Information
Helper Utilities
The methods in DatabaseContext
rely on helper utilities like SQLUtility
for the actual execution of commands. Ensure these utilities are implemented and properly handle:
- Parameter validation.
- SQL injection prevention.
- Edge cases (e.g., null results, empty queries).
Dynamic Return Types
Methods returning IEnumerable<dynamic>
can be used for flexible but untyped results:
var result = connection.Query("SELECT * FROM Users");
Error Handling
Always handle potential exceptions from database operations:
try
{
var data = connection.ExecuteScalar<int>("SP_GetCount", new { Id = 1 });
}
catch (SqlException ex)
{
// Handle SQL exceptions
}
catch (Exception ex)
{
// Handle other exceptions
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 | 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. |
-
.NETStandard 2.0
- Microsoft.CSharp (>= 4.5.0)
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 | |
---|---|---|---|
4.0.0 | 43 | 11/29/2024 | |
3.1.1 | 163 | 9/14/2024 | |
3.0.0 | 102 | 9/6/2024 | |
2.0.9 | 121 | 9/4/2024 | |
2.0.8 | 113 | 9/4/2024 | |
2.0.7 | 111 | 9/4/2024 | |
2.0.6 | 111 | 9/4/2024 | |
2.0.5 | 108 | 8/17/2024 | |
2.0.4 | 296 | 10/15/2023 | |
2.0.3 | 156 | 10/15/2023 | |
2.0.2 | 151 | 10/13/2023 | |
2.0.1 | 149 | 10/13/2023 | |
2.0.0 | 147 | 10/10/2023 | |
1.0.7 | 169 | 10/7/2023 | |
1.0.6 | 189 | 7/13/2023 | |
1.0.5 | 199 | 7/9/2023 | |
1.0.4 | 195 | 7/7/2023 | |
1.0.3 | 200 | 7/6/2023 | |
1.0.2 | 191 | 7/6/2023 | |
1.0.1 | 191 | 7/6/2023 | |
1.0.0 | 192 | 7/6/2023 |