KI-Kunst-Kirsten-Kloeckner/DataAccess/DbAccess/SqlDataAccess.cs

34 lines
1.1 KiB
C#
Raw Normal View History

2023-10-10 16:21:00 +02:00
using Microsoft.Extensions.Configuration;
using System.Data.SqlClient;
using System.Data;
using Dapper;
namespace DataAccess.DbAccess;
/// <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;
}
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));
return await connection.QueryAsync<TResult>(storedProcedure, parameters, commandType: CommandType.StoredProcedure);
2023-10-10 16:21:00 +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);
}
}