PostgreSQL La base de donnees la plus sophistiquee au monde.

Forums PostgreSQL.fr

Le forum officiel de la communauté francophone de PostgreSQL

Vous n'êtes pas identifié(e).

#1 30/11/2021 17:51:33

LowiM
Membre

Ouvrir une base de données sous windows forms avec npgsql

Bonjour je m'appelle Thibaut ,je suis actuellement en deuxième année de BTS SIO et j'aurais besoin d'aides concernant un projet de fin d'année .
Pour résumer je souhaite utiliser une class requete que j'ai fais et qui ressemble à ce que je vais envoyer et pouvoir l'ouvrir dans un de mes forms afin de l'utiliser pour insérer supprimer et modifier des données sur l'application (et que ça modifie dans ma base postgresql)
Merci d'avance !! smile
Voici ma class Requete
----------------------------------------------------------------------------
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;
using Npgsql;
using System.Collections.Generic;
using Npgsql;
using NpgsqlTypes;
using System.Linq;
using System.Data;
using System.Text;
namespace PPE1_S1
{
    public class Requete
        {
        private static string Conx = "Server=localhost;Port=5432;Database=repertoire;User Id=postgres;Password=postgres;";
        NpgsqlCommand MyCmd = null;
        NpgsqlConnection MyCnx = null;
        public OleDbConnection oconn = new OleDbConnection(Conx);

        public bool OuvreLaBase()
        {
            bool ok = true;
            try
            {
                oconn.Open();
            }
            catch (Exception exc)
            {
                MessageBox.Show("Requetes-OuvreLaBase", exc.Message.ToString());
                ok = false;
            }
            return ok;
        }

        public void FermeLaBase()
        {
            try
            {
                oconn.Close();
            }
            catch (Exception exc)
            {
                MessageBox.Show("Requetes-FermeLaBase", exc.Message.ToString());
            }
        }

        public OleDbDataReader Extraction(string strSql, string ordre, string type)
        {
            OleDbCommand oCmd = new OleDbCommand(strSql, oconn);
            oCmd.CommandText = strSql;
            oCmd.CommandType = CommandType.Text;
            OleDbDataReader oDr = null;

            try
            {
                oDr = oCmd.ExecuteReader();
            }
            catch (Exception exc)
            {
                MessageBox.Show("Extraction",  exc.Message.ToString());
            }
            return oDr;
        }

        public int ExtractionSimple(string strSql, string ordre, string type)
        {
            int n = -1;
            OleDbCommand oCmd = new OleDbCommand(strSql, oconn);
            oCmd.CommandText = strSql;
            oCmd.CommandType = CommandType.Text;
            try
            {
                n = (int)oCmd.ExecuteScalar();
            }

            catch (Exception exc)
            {
                MessageBox.Show("ExtractionSimple", exc.Message.ToString());
            }
            return n;
        }

        public int Insertion(string strSql, string ordre, string type)
        {
            int n = -1;
            ExecuteRequete(strSql, ordre, type);
            return n;
        }

        public int Modification(string strSql, string ordre, string type)
        {
            int n = -1;
            ExecuteRequete(strSql, ordre, type);
            return n;
        }

        public int Suppression(string strSql, string ordre, string type)
        {
            int n = -1;
            ExecuteRequete(strSql, ordre, type);
            return n;
        }

        public int ExecuteRequete(string strSql, string ordre, string type)
        {
            int n = -1;
            OleDbCommand oCmd = new OleDbCommand(strSql, oconn);
            oCmd.CommandText = strSql;
            oCmd.CommandType = CommandType.Text;

            try
            {
                n = oCmd.ExecuteNonQuery();
            }

            catch (Exception exc)
            {
                MessageBox.Show("ExecuteRequete", exc.Message.ToString());
            }
            return n;
        }

        public OleDbDataReader RequeteStockee(string strSql, string ordre, string type)
        {
            OleDbCommand oCmd = new OleDbCommand(strSql, oconn);
            oCmd.CommandText = strSql;
            oCmd.CommandType = CommandType.StoredProcedure;
            OleDbDataReader oDr = null;

            try
            {
                oDr = oCmd.ExecuteReader();
            }
            catch (Exception exc)
            {
                MessageBox.Show("RequeteStockee", exc.Message.ToString());
            }
            return oDr;
        }
    }
}

Hors ligne

Pied de page des forums