DbConnectionMate 1.0.1
dotnet add package DbConnectionMate --version 1.0.1
NuGet\Install-Package DbConnectionMate -Version 1.0.1
<PackageReference Include="DbConnectionMate" Version="1.0.1" />
paket add DbConnectionMate --version 1.0.1
#r "nuget: DbConnectionMate, 1.0.1"
// Install DbConnectionMate as a Cake Addin #addin nuget:?package=DbConnectionMate&version=1.0.1 // Install DbConnectionMate as a Cake Tool #tool nuget:?package=DbConnectionMate&version=1.0.1
DbConnectionMate
DbConnectionMate is a lightweight and intuitive library that extends the IDbConnection
interface, simplifying the execution of basic SQL commands in .NET applications. It encapsulates common tasks like opening connections, managing transactions, and handling parameters, allowing you to focus on your application's core logic.
Key Features
- Simplified SQL Execution: Easily execute queries, commands, and stored procedures without repetitive boilerplate code.
- Parameter Management: Automatically handles parameters to make queries secure and concise.
- Transaction Support: Streamlines the use of database transactions for consistent and reliable data operations.
- Code Clarity: Reduces complexity in your database access code for better readability and maintainability.
- Flexible and Extensible: Works seamlessly with various ADO.NET providers and can be extended for custom use cases.
Installation
Install the package via NuGet Package Manager:
dotnet add package DbConnectionMate
ExecuteAsync
Method
Summary
Executes a command asynchronously using the provided IDbConnection
.
Parameters
IDbConnection connection
: The database connection to use.string? commandText
: The command text to execute.IsolationLevel isolationLevel
: [Optional] The isolation level for the transaction. Default isIsolationLevel.ReadCommitted
.IEnumerable<IDbDataParameter>? parameters
: [Optional] The parameters to add to the command. Default isnull
.CancellationToken cancellationToken
: [Optional] The cancellation token to cancel the operation. Default isdefault
.
Returns
A task representing the asynchronous operation, with the number of rows affected.
Exceptions
ArgumentNullException
: Thrown whencommandText
is null.
Example
public class Example
{
public async Task ExecuteSqlWithParamsAsync()
{
string connectionString = "your_connection_string";
using (IDbConnection connection = new SqlConnection(connectionString))
{
var parameters = new List<SqlParameter> { new SqlParameter("@Name", "John Doe"), new SqlParameter("@Age", 30) };
int rowsAffected =
await connection.ExecuteAsync("INSERT INTO Users (Name, Age) VALUES (@Name, @Age);",
parameters);
Console.WriteLine($"Rows affected: {rowsAffected}");
}
}
}
ExecuteScalarAsync<T>
Method
Summary
Executes a scalar command asynchronously using the provided IDbConnection
.
Type Parameters
T
: The type of the result.
Parameters
IDbConnection connection
: The database connection to use.string? commandText
: The command text to execute.IsolationLevel isolationLevel
: [Optional] The isolation level for the transaction. Default isIsolationLevel.ReadCommitted
.IEnumerable<IDbDataParameter>? parameters
: [Optional] The parameters to add to the command. Default isnull
.CancellationToken cancellationToken
: [Optional] The cancellation token to cancel the operation. Default isdefault
.
Returns
A task representing the asynchronous operation, with the result of the scalar command.
Exceptions
ArgumentNullException
: Thrown whencommandText
is null.
Example
public class Example
{
public async Task<DateTime> ExecuteScalarAsync()
{
string connectionString = "your_connection_string";
using (IDbConnection connection = new SqlConnection(connectionString))
{
var result = DateTime.MinValue;
result = await connection.ExecuteScalarAsync<DateTime>("SELECT GETDATE();");
return result;
}
}
}
ExecuteReaderAsync
Method
Summary
Executes a command asynchronously using the provided IDbConnection
and processes the result with a data reader.
Parameters
IDbConnection connection
: The database connection to use.string? commandText
: The command text to execute.IsolationLevel isolationLevel
: [Optional] The isolation level for the transaction. Default isIsolationLevel.ReadCommitted
.IEnumerable<IDbDataParameter>? parameters
: [Optional] The parameters to add to the command. Default isnull
.CancellationToken cancellationToken
: [Optional] The cancellation token to cancel the operation. Default isdefault
.
Returns
A task representing the asynchronous operation, with the result being an enumerable of dictionaries where each dictionary represents a row with column names as keys and column values as values.
Exceptions
ArgumentNullException
: Thrown whencommandText
is null.
Example
public class Example
{
public async Task<List<Movie>> ExecuteReaderAsync()
{
string connectionString = "your_connection_string";
using (IDbConnection conn = new SqlConnection(connectionString))
{
var result = new List<Movie>();
var reader = await conn.ExecuteReaderAsync("SELECT Title, Director, ReleaseYear From Movies;");
while (await reader.ReadAsync())
{
var movie = new Movie
{
Title = reader["Title"].ToString(),
Director = reader["Director"].ToString(),
ReleaseYear = Convert.ToInt32(reader["ReleaseYear"])
};
result.Add(movie);
}
return result;
}
}
}
public class Movie
{
public string Title { get; set; }
public string Director { get; set; }
public int ReleaseYear { get; set; }
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net9.0
- No dependencies.
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.0.1 | 96 | 12/22/2024 |