Code cleanup

This commit is contained in:
2023-04-12 22:29:49 +02:00
parent b100eca490
commit 7a036b3404
22 changed files with 184 additions and 183 deletions

View File

@@ -0,0 +1,49 @@
using Npgsql;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SewerStammGen.DAL.Services
{
public class PostgresqlDataService : IDisposable
{
string connString = "Host = localhost; Database = SewerGen; Username = SewerGen; Password = SewerGen";
NpgsqlDataSource dataSource;
protected NpgsqlConnection? conn = null;
private string tableName = "";
public PostgresqlDataService(string tableName)
{
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString);
dataSource = dataSourceBuilder.Build();
conn = dataSource.OpenConnection();
this.tableName = tableName;
}
public virtual async Task<bool> Delete(int id)
{
string command = $"DELETE "+tableName+" WHERE \"Id\" = @1";
await using (var cmd = new NpgsqlCommand(command, conn))
{
cmd.Parameters.AddWithValue("1", id);
int res = await cmd.ExecuteNonQueryAsync();
}
return true;
}
public void Dispose()
{
if(conn != null && conn.State == System.Data.ConnectionState.Open)
{
conn.Close();
conn.Dispose();
}
}
}
}