2023-10-10 16:21:00 +02:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using Dapper;
|
|
|
|
|
|
|
|
|
|
namespace DataAccess.DbAccess;
|
|
|
|
|
|
2023-10-10 18:56:05 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Bietet lesenden und schreibenden Zugriff auf eine Datenbank.
|
|
|
|
|
/// </summary>
|
2023-10-10 16:21:00 +02:00
|
|
|
|
public class SqlDataAccess : ISqlDataAccess
|
|
|
|
|
{
|
|
|
|
|
private readonly IConfiguration _config;
|
|
|
|
|
|
|
|
|
|
public SqlDataAccess(IConfiguration config)
|
|
|
|
|
{
|
|
|
|
|
_config = config;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-10 18:56:05 +02:00
|
|
|
|
public async Task<IEnumerable<TResult>> LoadData<TResult, TParameter>(string storedProcedure, TParameter parameters, string connectionId = "Default")
|
2023-10-10 16:21:00 +02:00
|
|
|
|
{
|
|
|
|
|
using IDbConnection connection = new SqlConnection(_config.GetConnectionString(connectionId));
|
|
|
|
|
|
2023-10-10 18:56:05 +02:00
|
|
|
|
return await connection.QueryAsync<TResult>(storedProcedure, parameters, commandType: CommandType.StoredProcedure);
|
2023-10-10 16:21:00 +02:00
|
|
|
|
}
|
2023-10-10 18:18:17 +02:00
|
|
|
|
|
2023-10-10 18:56:05 +02:00
|
|
|
|
public async Task SaveData<TParameter>(string storedProcedure, TParameter parameters, string connectionId = "Default")
|
2023-10-10 16:21:00 +02:00
|
|
|
|
{
|
|
|
|
|
using IDbConnection connection = new SqlConnection(_config.GetConnectionString(connectionId));
|
|
|
|
|
|
|
|
|
|
await connection.ExecuteAsync(storedProcedure, parameters, commandType: CommandType.StoredProcedure);
|
|
|
|
|
}
|
|
|
|
|
}
|