SafeConnection 1.0.11

dotnet add package SafeConnection --version 1.0.11
                    
NuGet\Install-Package SafeConnection -Version 1.0.11
                    
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="SafeConnection" Version="1.0.11" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SafeConnection" Version="1.0.11" />
                    
Directory.Packages.props
<PackageReference Include="SafeConnection" />
                    
Project file
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 SafeConnection --version 1.0.11
                    
#r "nuget: SafeConnection, 1.0.11"
                    
#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 SafeConnection@1.0.11
                    
#: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=SafeConnection&version=1.0.11
                    
Install as a Cake Addin
#tool nuget:?package=SafeConnection&version=1.0.11
                    
Install as a Cake Tool

About

SafeConnection is a robust library designed to facilitate secure and efficient database connections. It provides a comprehensive set of features including:

  • Secure Connections: Ensures all database connections are secure and encrypted. Additionally, it closes the passed connection explicitly.
  • Caching: Implements in-memory caching to reduce database load and improve performance.
  • Logging: Integrated logging capabilities to track database operations and errors.
  • Compatibility: Supports multiple .NET versions including .NET Standard 3.1 and .NET 6.0.
  • Ease of Use: Simple and intuitive API for quick integration into your projects.

This library is ideal for applications that require reliable and secure database interactions with minimal overhead.

Usage Instructions

To use SafeConnection, simply install the package via NuGet and add the following code to your project:

  1. Install the Package: Add the SafeConnection package to your project using NuGet:

     dotnet add package SafeConnection
    
  2. Inject the Service: Inject the SafeConnection service into your application as Singleton:

     builder.Services.AddSingleton<SafeConnection>(_ => new SafeConnection(
     	appName: builder.Environment.ApplicationName, // It will be used in the logs.
     	environment: builder.Environment.EnvironmentName, // It will be used in the logs.
     	sqlConnectionString: builder.Configuration.GetConnectionString("Your_Sql_log_connectionString"), // Where the logs will be stored.
     	sqlTable: "App_Logs", // The table where the logs will be stored.
     	cacheExpirationMinutes: builder.Configuration.GetValue<int>("CacheExpirationInMinutes") // The expiration time of the cache in minutes.
     ));
    
  3. Use the Service: Use the SafeConnection service in your application to interact with the database:

     public class YourService
     {
     	private readonly SafeConnection _safeConnection;
    
     	// Inject SafeConnection service
     	public YourService(SafeConnection safeConnection)
     	{
     		_safeConnection = safeConnection;
     	}
    
     	// Use SafeConnection to execute SQL queries
     	public async Task<IEnumerable<YourModel>> GetYourData()
     	{
     		var query = @"SELECT field1, field2 from your_table";
    
            var connection = new OleDbConnection("Your__query_string");		
     		return await _safeConnection.QueryAsync<YourModel>(connection, query, "YourCacheKey", queryParams: null, specificCache: null, commandTimeout: 30);
     	}
     }
    
  4. The SQL table structure:

     CREATE TABLE App_Logs (
     	Id int IDENTITY(1,1) NOT NULL,
     	[DateTime] datetime NOT NULL,
     	App varchar(20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
     	Env varchar(20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
     	Query varchar(500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
     	Cached bit NOT NULL,
     	Duration real NOT NULL,
     	[Rows] int NOT NULL,
     	Columns int NOT NULL,
     	Kb int NULL,
     	ServerName varchar(25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
     	ConnectionType varchar(25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
     	CONSTRAINT App_Logs_PK PRIMARY KEY (Id)
     );
    

Methods:

QueryAsync<T> Executes a query asynchronously and returns the result set.

Type Parameters
  • T: The type of the result set elements.
Parameters
  • IDbConnection dbConnection: The database connection to use for the query.
  • string query: The SQL query to execute.
  • string queryKey: A unique key identifying the query, used for caching the result and identifying the query in the logs.
  • object queryParams: The parameters to pass to the query. Default is null.
  • int? specificCache: The expiration time of the cache in minutes for this query. Default is null and uses the default cache expiration time. Use 0 to disable caching.
  • int? commandTimeout: The command timeout in seconds. Default is null.
Returns
  • Task<IEnumerable<T>>: An enumerable of the result set elements.
Example
var parameters = new { Id = 1 }; // Optional parameter
var query = @"SELECT field1, field2 from your_table WHERE Id = @Id";
var specificCache = 0; // Optional: 0 to disable cache, null for default, or specific minutes
var commandTimeout = 30; // Optional: timeout in seconds, null for default
var connection = new OleDbConnection("Your__query_string");
return await _safeConnection.QueryAsync<YourModel>(connection, query, "YourCacheKey", parameters, specificCache, commandTimeout);

Query<T> Executes a query and returns the result set.
Type Parameters
  • T: The type of the result set elements.
Parameters
  • IDbConnection dbConnection: The database connection to use for the query.
  • string query: The SQL query to execute.
  • string queryKey: A unique key identifying the query, used for caching the result and identifying the query in the logs.
  • object queryParams: The parameters to pass to the query. Default is null.
  • int? specificCache: The expiration time of the cache in minutes for this query. Default is null and uses the default cache expiration time. Use 0 to disable caching.
  • int? commandTimeout: The command timeout in seconds. Default is null.
Returns
  • IEnumerable<T>: An enumerable of the result set elements.
Example
var parameters = new { Id = 1 }; // Optional parameter
var query = @"SELECT field1, field2 from your_table WHERE Id = @Id";
var specificCache = 0; // Optional: 0 to disable cache, null for default, or specific minutes
var commandTimeout = 30; // Optional: timeout in seconds, null for default
var connection = new OleDbConnection("Your__query_string");
return _safeConnection.Query<YourModel>(connection, query, "YourCacheKey", parameters, specificCache, commandTimeout);

LogInformation Manually logs information.
Parameters
  • string queryKey: A unique key identifying the query, it will be used to identify the query in the logs.
  • long elapsedMilliseconds: The time taken to execute the query, in milliseconds.
  • int rowCount: The number of rows returned by the query.
  • int columnCount: The number of columns in the result set.
  • int kb: The size of the result set in kilobytes. Default is -1 if not specified.
  • string serverName: The name of the server where the query was executed. Default is an empty string if not specified.
  • string connectionType: The type of the connection used to execute the query. Default is an empty string if not specified.
Example
var queryKey = "YourCacheKey";
var elapsedMilliseconds = 100;
var rowCount = 10;
var columnCount = 2;
var kb = 1;
var serverName = "YourServerName"; // Optional parameter, default is an empty string.
var connectionType = "YourConnectionType"; // Optional parameter, default is an empty string.
_safeConnection.LogInformation(queryKey, elapsedMilliseconds, rowCount, columnCount, kb, serverName, connectionType);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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.  net9.0 was computed.  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.  net10.0 was computed.  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. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.11 145 7/1/2026
1.0.10 182 1/8/2026
1.0.9 163 1/8/2026
1.0.8 326 8/20/2024
1.0.7 259 8/16/2024
1.0.6 227 8/16/2024
1.0.5 225 8/16/2024
1.0.4 231 8/16/2024
1.0.3 240 8/16/2024
1.0.1 231 8/16/2024
1.0.0 241 8/14/2024