Kothf.Data
2.10.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Kothf.Data --version 2.10.0
NuGet\Install-Package Kothf.Data -Version 2.10.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Kothf.Data" Version="2.10.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Kothf.Data" Version="2.10.0" />
<PackageReference Include="Kothf.Data" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Kothf.Data --version 2.10.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Kothf.Data, 2.10.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Kothf.Data@2.10.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Kothf.Data&version=2.10.0
#tool nuget:?package=Kothf.Data&version=2.10.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Kothf.Data
Kothf.Data is a small ADO.NET helper library that provides a lightweight Database abstraction, an IDatabase interface, and a SqlHelper utility for creating connections, commands, and parameters.
Usage
Usage of Database
public async Task ExecuteTransactionAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Starts to test `ExecuteTransactionAsync`");
try
{
DbParameter[] parameters1 = [
_database.CreateParameter("@p11", DbType.String, "usr001")
];
string sql1 = "DELETE FROM [User] WHERE UserCode = @p11;";
DbParameter[] parameters2 = [
_database.CreateParameter("@p21", DbType.String, "usr001"),
_database.CreateParameter("@p22", DbType.String, "Tom"),
_database.CreateParameter("@p23", DbType.String, "W3Rb3AoWjXs="),
_database.CreateParameter("@p24", DbType.String, "tom@gmail.com"),
_database.CreateParameter("@p25", DbType.String, "18618618666")
];
string sql2 = $$"""
INSERT INTO [User] (UserCode,UserName,Password,Email,Phone,Attributes,[State],CreatedTime,LastModifiedTime) VALUES
(@p21,@p22,@p23,@p24,@p25,1,1,GETDATE(),GETDATE());
""";
try
{
int affectedRows;
await _database.BeginTransactionAsync(cancellationToken: stoppingToken);
_logger.LogInformation("Starts to execute sql1");
affectedRows = await _database.ExecuteNonQueryAsync(CommandType.Text, sql1, parameters1, stoppingToken);
_logger.LogInformation("Removing User successfully. User code: {userCode}, affected records: {count}", "usr001", affectedRows);
_logger.LogInformation("Starts to execute sql2");
affectedRows = await _database.ExecuteNonQueryAsync(CommandType.Text, sql2, parameters2, stoppingToken);
_logger.LogInformation("Adding User successfully. User code: {userCode}, affected records: {count}", "usr001", affectedRows);
await _database.CommitAsync(stoppingToken);
}
catch (Exception)
{
await _database.RollbackAsync(stoppingToken);
throw;
}
}
catch (Exception ex)
{
_logger.LogError("Unhandled exception occurred during testing:\n{exception}", ex.ToString());
}
_logger.LogInformation("Finished Testing `ExecuteTransactionAsync`");
}
Usage of SqlHelper
public async Task ExecuteTransactionAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Starts to test `ExecuteTransactionAsync`");
try
{
DbParameter[] parameters1 = [
SqlHelper.CreateParameter(ProviderInvariantName, "@p11", DbType.String, "usr001")
];
string sql1 = "DELETE FROM [User] WHERE UserCode = @p11;";
DbParameter[] parameters2 = [
SqlHelper.CreateParameter(ProviderInvariantName, "@p21", DbType.String, "usr001"),
SqlHelper.CreateParameter(ProviderInvariantName, "@p22", DbType.String, "Tom"),
SqlHelper.CreateParameter(ProviderInvariantName, "@p23", DbType.String, "W3Rb3AoWjXs="),
SqlHelper.CreateParameter(ProviderInvariantName, "@p24", DbType.String, "tom@gmail.com"),
SqlHelper.CreateParameter(ProviderInvariantName, "@p25", DbType.String, "18618618666")
];
string sql2 = $$"""
INSERT INTO [User] (UserCode,UserName,Password,Email,Phone,Attributes,[State],CreatedTime,LastModifiedTime) VALUES
(@p21,@p22,@p23,@p24,@p25,1,1,GETDATE(),GETDATE());
""";
using var connection = await SqlHelper.CreateConnectionAndOpenAsync(ConnectionString, ProviderInvariantName, stoppingToken);
using var transaction = await connection.BeginTransactionAsync(stoppingToken);
int affectedRows = 0;
try
{
_logger.LogInformation("Start to execute sql1");
using var command = SqlHelper.CreateCommand(null, transaction, CommandType.Text, sql1, parameters1);
affectedRows = await SqlHelper.ExecuteNonQueryAsync(command, stoppingToken);
_logger.LogInformation("Removing User successfully. User code: {userCode}, affected records: {count}", "usr001", affectedRows);
_logger.LogInformation("Start to execute sql2");
_ = SqlHelper.ReuseCommand(command, CommandType.Text, sql2, parameters2);
affectedRows = await SqlHelper.ExecuteNonQueryAsync(command, stoppingToken);
_logger.LogInformation("Adding User successfully. User code: {userCode}, affected records: {count}", "usr001", affectedRows);
await transaction.CommitAsync(stoppingToken);
}
catch (Exception)
{
await transaction.RollbackAsync(stoppingToken);
throw;
}
}
catch (Exception ex)
{
_logger.LogError("Unhandled exception occurred during testing:\n{exception}", ex.ToString());
}
_logger.LogInformation("Finished Testing `ExecuteTransactionAsync`");
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.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.