Fusion de Connexion dans Npgsql
continuous-integration/drone/push Build is failing Details

Link_to_npgsql
Hug0lvt 2 years ago
commit 65caf7d7b2

@ -15,7 +15,7 @@ namespace IHM
/// </summary> /// </summary>
public partial class App : Application public partial class App : Application
{ {
public Manager AllInscrits { get; private set; } = new Manager(new LinqToPgSQL.PersLinqToPgSQL()); public Manager Manager { get; private set; } = new Manager(new LinqToPgSQL.PersLinqToPgSQL());
//public Manager AllInscrits { get; private set; } = new Manager(new Stub()); //public Manager AllInscrits { get; private set; } = new Manager(new Stub());
} }

@ -7,7 +7,12 @@
mc:Ignorable="d" mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"> Title="MainWindow" Height="450" Width="800">
<Grid> <Grid>
<Button x:Name="test2" Content="Button" HorizontalAlignment="Left" Margin="411,167,0,0" VerticalAlignment="Top" Click="test_Click"/> <TextBlock Name="idpers" Text="null"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<TextBox Name="id" Height="100" Width="200" Text="OUIOUIOUI"/>
<TextBox Name="mdp" Height="100" Width="200" Text="OUIOUIOUI"/>
</StackPanel>
<Button x:Name="test2" Content="Button" HorizontalAlignment="Center" Margin="0,320,0,0" VerticalAlignment="Top" Click="test_Click"/>
</Grid> </Grid>

@ -22,40 +22,21 @@ namespace IHM
/// </summary> /// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
public Manager ListedesInscrits => ((App)Application.Current).AllInscrits; public Manager Manager => ((App)Application.Current).Manager;
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
DataContext = Manager.SelectedInscrits;
ListedesInscrits.LoadInscrit();
DataContext = ListedesInscrits;
} }
public void testSelect() public void testSelect()
{ {
foreach (Inscrit i in ListedesInscrits.ListedesInscrits) MessageBox.Show($"{Manager.SelectedInscrits}");
{
MessageBox.Show($"{i.Id} + {i.Nom} + {i.Prenom} + {i.Mail} + {i.Mdp}");
}
} }
public void testSuppression() public void testSuppression()
{ {
foreach (Inscrit i in ListedesInscrits.ListedesInscrits)
{
if (i.Nom == "YOUVOI")
{
ListedesInscrits.supprimerInscritBdd(i);
}
}
MessageBox.Show("Suppression ok"); MessageBox.Show("Suppression ok");
} }
@ -75,9 +56,13 @@ namespace IHM
private void test_Click(object sender, RoutedEventArgs e) private void test_Click(object sender, RoutedEventArgs e)
{ {
// testSelect(); Manager.LoadInscrit(id.Text,mdp.Text);
// testSuppression(); if (Manager.SelectedInscrits != null)
testSupressionBanqueBdd(); {
idpers.Text = Manager.SelectedInscrits;
testSelect();
}
else idpers.Text = "";
} }
} }

@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Data;
using System.Configuration;
using Npgsql; using Npgsql;
using Model; using Model;
using System.IO; using System.IO;
@ -14,49 +16,31 @@ namespace LinqToPgSQL
{ {
public class PersLinqToPgSQL : IPersistanceManager public class PersLinqToPgSQL : IPersistanceManager
{ {
private static string Host = "90.114.135.116"; string connexionBDD = String.Format("Server=90.114.135.116; Username=postgres; Database=conseco; Port=5432; Password=lulu; SSLMode=Prefer");
private static string User = "postgres"; public string LoadInscrit(string id, string mdp)
private static string DBname = "conseco";
private static string Password = "lulu";
private static string Port = "5432";
string connString =
String.Format(
"Server={0};Username={1};Database={2};Port={3};Password={4};SSLMode=Prefer",
Host,
User,
DBname,
Port,
Password);
public PersLinqToPgSQL() { }
public IEnumerable<Inscrit> LoadInscrit()
{ {
List<Inscrit> ListeInscrits = new List<Inscrit>(); string resultat="";
var conn = new NpgsqlConnection(connexionBDD);
var conn = new NpgsqlConnection(connString);
Console.Out.WriteLine("Ouverture de la connection"); Console.Out.WriteLine("Ouverture de la connection");
conn.Open();
NpgsqlParameter p1 = new NpgsqlParameter { ParameterName = "p", Value = id };
NpgsqlParameter p2 = new NpgsqlParameter { ParameterName = "p2", Value = mdp };
NpgsqlCommand cmd = new NpgsqlCommand($"SELECT id FROM INSCRIT WHERE (nom=(@p) OR mail=(@p)) AND mdp=@p2", conn);
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
NpgsqlDataReader dr = cmd.ExecuteReader();
try try
{ {
conn.Open(); dr.Read();
resultat = dr.GetString(0);
dr.Close();
return resultat;
} }
catch catch (Exception ex)
{ {
conn.Close(); MessageBox.Show(ex+"Utilisateur inconnu");
dr.Close();
MessageBox.Show("Problème de connection à la base de données. L'application se fermera après fermeture de la fenêtre"); return "null";//a changer doit retester
Environment.Exit(0);
}
NpgsqlDataReader dbReader = new NpgsqlCommand("SELECT * FROM Inscrit", conn).ExecuteReader();
while (dbReader.Read())
{
ListeInscrits.Add(new Inscrit(dbReader.GetString(0), dbReader.GetString(1), dbReader.GetString(2), dbReader.GetString(3), dbReader.GetString(4)));
} }
@ -140,8 +124,9 @@ namespace LinqToPgSQL
/*Suppression d'un inscrit dans la base de données*/ /*Suppression d'un inscrit dans la base de données*/
public async void SupprimerInscritBdd(Inscrit i) public async void SupprimerInscritBdd(Inscrit i)
{ {
/*List<Inscrit> ListeInscrits = new List<Inscrit>(LoadInscrit());*/
var conn = new NpgsqlConnection(connString); var conn = new NpgsqlConnection(connexionBDD);
Console.Out.WriteLine("Ouverture de la connection"); Console.Out.WriteLine("Ouverture de la connection");
try try
{ {
@ -156,8 +141,8 @@ namespace LinqToPgSQL
string requete = $"DELETE FROM INSCRIT WHERE id=(@p)"; string requete = $"DELETE FROM INSCRIT WHERE id=(@p)";
string requeteFKey = $"DELETE FROM DEVISEINSCRIT WHERE idInscrit=(@p2)";
using (var command1 = new NpgsqlCommand(requete, conn)) using (var command1 = new NpgsqlCommand(requeteFKey, conn))
{ {
command1.Parameters.AddWithValue("p", i.Id); command1.Parameters.AddWithValue("p", i.Id);
await command1.ExecuteNonQueryAsync(); await command1.ExecuteNonQueryAsync();
@ -189,8 +174,8 @@ namespace LinqToPgSQL
string requete = $"DELETE * FROM BANQUE b, INSCRBANQUE ib WHERE b.nom=ib.nomBanque AND ib.idInscrit=(@id)"; string requete = $"DELETE * FROM BANQUE b, INSCRBANQUE ib WHERE b.nom=ib.nomBanque AND ib.idInscrit=(@id)";
using (var command1 = new NpgsqlCommand(requete, conn)) using (var command1 = new NpgsqlCommand(requete, conn))
{ {
command1.Parameters.AddWithValue("id", i.Id.ToString()); command.Parameters.AddWithValue("p", i.Id);
await command1.ExecuteNonQueryAsync(); await command.ExecuteNonQueryAsync();
} }
} }

@ -8,8 +8,7 @@ namespace Model
{ {
public interface IPersistanceManager public interface IPersistanceManager
{ {
IEnumerable<Inscrit> LoadInscrit(); string LoadInscrit(string id,string mdp);
IEnumerable<Banque> LoadBanque();
void SupprimerInscritBdd(Inscrit inscrit); void SupprimerInscritBdd(Inscrit inscrit);
void SupprimerBanqueBdd(Inscrit inscrit, Banque banque); void SupprimerBanqueBdd(Inscrit inscrit, Banque banque);
void SupprimerToutesBanquesBdd(Inscrit inscrit); void SupprimerToutesBanquesBdd(Inscrit inscrit);

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
using Npgsql;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -10,31 +11,9 @@ namespace Model
{ {
public class Manager : INotifyPropertyChanged public class Manager : INotifyPropertyChanged
{ {
public Manager() { }
public event PropertyChangedEventHandler? PropertyChanged; public event PropertyChangedEventHandler? PropertyChanged;
public IReadOnlyCollection<Inscrit> ListedesInscrits { get; private set; }
private List<Inscrit> TousLesInscrits { get; set; } = new List<Inscrit>();
public IReadOnlyCollection<Banque> ListeDesBanques { get; private set; }
private List<Banque> TouteLesBanques { get; set; } = new List<Banque>();
public IPersistanceManager Pers { get; private set; } public IPersistanceManager Pers { get; private set; }
public string SelectedInscrits;
public Inscrit SelectedInscrits
{
get => selectedInscrits;
set
{
if (selectedInscrits != value)
{
selectedInscrits = value;
OnPropertyChanged(nameof(SelectedInscrits));
}
}
}
private Inscrit selectedInscrits;
public Banque SelectedBanque public Banque SelectedBanque
{ {
@ -52,39 +31,19 @@ namespace Model
void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
public void LoadInscrit()
{
TousLesInscrits.Clear();
TousLesInscrits.AddRange(Pers.LoadInscrit());
if (TousLesInscrits.Count > 0)
SelectedInscrits = TousLesInscrits.First();
}
public void LoadBanque()
{
TouteLesBanques.Clear();
TouteLesBanques.AddRange(Pers.LoadBanque());
if (TouteLesBanques.Count > 0)
SelectedBanque = TouteLesBanques.First();
}
public Manager(IPersistanceManager persistance) public Manager(IPersistanceManager persistance)
{ {
ListedesInscrits = new ReadOnlyCollection<Inscrit>(TousLesInscrits);
ListeDesBanques = new ReadOnlyCollection<Banque>(TouteLesBanques);
Pers = persistance; Pers = persistance;
} }
public void supprimerInscritBdd(Inscrit inscrit) public void SupprimerInscritBdd(Inscrit i)
{ {
Pers.SupprimerInscritBdd(inscrit); Pers.SupprimerInscritBdd(inscrit);
} }
public void supprimerBanqueBdd(Inscrit inscrit, Banque banque) public void LoadInscrit(string id, string mdp)
{ {
Pers.SupprimerBanqueBdd(inscrit, banque); SelectedInscrits = Pers.LoadInscrit(id, mdp);
} }
public void supprimerToutesBanquesBdd(Inscrit inscrit) public void supprimerToutesBanquesBdd(Inscrit inscrit)

@ -6,4 +6,8 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql" Version="6.0.7" />
</ItemGroup>
</Project> </Project>

Loading…
Cancel
Save