Example #1
0
        public static bool addOrUpdatePersonne(MyPersonne personne)
        {
            DataRow row = personnesDS.Tables["Personne"].Rows.Find(personne.Id);

            if (row == null)
            {
                DataRow newrow = personnesDS.Tables["Personne"].NewRow();
                LoadPersonnelFromRow(personne, newrow);
                personnesDS.Tables["Personne"].Rows.Add(newrow);

                bool b = adapter.Update(personnesDS.Tables["Personne"]) == 1;
                if (b)
                {
                    personnesDS.Tables["Personne"].Clear();
                    adapter.Fill(personnesDS, "Personne");
                }


                return(b);
            }
            else
            {
                return(Update(personne, row));
            }
        }
Example #2
0
        public MyPersonne getPersonneById(int id)
        {
            using (SqlConnection cnx = new SqlConnection(ittacours))
            {
                peuple.Clear();
                cnx.Open();
                string       sql     = @"Select [Id], [Nom] ,[Prenom], [Salaire], [DateNaissance] from dbo.Personne
                                where [Id] = @id";
                SqlCommand   command = new SqlCommand(sql, cnx);
                SqlParameter sp      = new SqlParameter("@id", id);
                command.Parameters.Add(sp);
                SqlDataReader reader = command.ExecuteReader();
                try
                {
                    while (reader.Read())
                    {
                        MyPersonne p = new MyPersonne();
                        p.Id     = (int)reader["Id"];
                        p.Nom    = (String)reader["Nom"];
                        p.Prenom = (String)reader["Prenom"];
                        Object o = reader["Salaire"];
                        p.Salaire       = (float)(o != DBNull.Value ? (decimal)o : 0);
                        p.DateNaissance = (DateTime?)(reader["DateNaissance"] == DBNull.Value ? null : reader["DateNaissance"]);

                        return(p);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("get by ID pas potible", ex);
                }
                return(null);
            }
        }
Example #3
0
        public List <MyPersonne> getPersonnes()
        {
            try
            {
                using (SqlConnection cnx = new SqlConnection(ittacours))
                {
                    peuple.Clear();

                    cnx.Open();
                    string        sql     = @"Select [Id], [Nom] ,[Prenom], [Salaire], [DateNaissance] from dbo.Personne";
                    SqlCommand    command = new SqlCommand(sql, cnx);
                    SqlDataReader reader  = command.ExecuteReader();
                    while (reader.Read())
                    {
                        MyPersonne p = new MyPersonne();
                        p.Id            = (int)reader["Id"];
                        p.Nom           = (String)reader["Nom"];
                        p.Prenom        = (String)reader["Prenom"];
                        p.Salaire       = reader["Salaire"] == null ? (float)reader["Salaire"] : 0;
                        p.DateNaissance = (DateTime?)(reader["DateNaissance"] == DBNull.Value ? null : reader["DateNaissance"]);
                        peuple.Add(p);
                    }
                    return(peuple);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("select all pas potible", ex);
            }
        }
Example #4
0
 public bool Update(MyPersonne personne)
 {
     try
     {
         using (SqlConnection cnx = new SqlConnection(ittacours))
         {
             cnx.Open();
             string       sql     = @"update dbo.Personne
                 set nom= @nom, prenom= @prenom, salaire= @salaire, datenaissance= @datenaissance
                 where id=@id";
             SqlCommand   command = new SqlCommand(sql, cnx);
             SqlParameter sp      = new SqlParameter("@nom", personne.Nom);
             SqlParameter sp1     = new SqlParameter("@prenom", personne.Prenom);
             SqlParameter sp2     = new SqlParameter("@salaire", personne.Salaire);
             sp2.DbType = System.Data.DbType.Double;
             SqlParameter sp3 = new SqlParameter("@datenaissance", (object)personne.DateNaissance ?? DBNull.Value);
             sp3.DbType = System.Data.DbType.DateTime2;
             SqlParameter sp4 = new SqlParameter("@id", personne.Id);
             command.Parameters.Add(sp);
             command.Parameters.Add(sp1);
             command.Parameters.Add(sp2);
             command.Parameters.Add(sp3);
             command.Parameters.Add(sp4);
             return(command.ExecuteNonQuery() == 1);
         }
     }
     catch (Exception ex)
     {
         throw new Exception("update pas potible", ex);
     }
 }
Example #5
0
 private static void LoadPersonnelFromRow(MyPersonne personne, DataRow row)
 {
     row["Id"]            = personne.Id;
     row["Nom"]           = personne.Nom;
     row["Prenom"]        = personne.Prenom;
     row["Salaire"]       = personne.Salaire;
     row["DateNaissance"] = (object)personne.DateNaissance ?? DBNull.Value;
 }
Example #6
0
        public static bool Update(MyPersonne personne, DataRow row = null)
        {
            if (row == null)
            {
                row = personnesDS.Tables["Personne"].Rows.Find(personne.Id);
            }

            LoadPersonnelFromRow(personne, row);

            return(adapter.Update(personnesDS.Tables["Personne"]) == 1);
        }
Example #7
0
        public static MyPersonne getPersonneById(int id)
        {
            DataRow    row = personnesDS.Tables["Personne"].Rows.Find(id);
            MyPersonne p   = new MyPersonne();

            p.Id            = (int)row["Id"];
            p.Nom           = (String)row["Nom"];
            p.Prenom        = (String)row["Prenom"];
            p.Salaire       = row["Salaire"] != DBNull.Value ? (float)(decimal)row["Salaire"] : 0;
            p.DateNaissance = (DateTime?)(row["DateNaissance"] == DBNull.Value ? null : row["DateNaissance"]);
            return(p);
        }
Example #8
0
        public static List <MyPersonne> getPersonnes()
        {
            List <MyPersonne> peuple = new List <MyPersonne>();

            foreach (DataRow row in personnesDS.Tables["Personne"].Rows)
            {
                MyPersonne p = new MyPersonne();
                p.Id            = (int)row["Id"];
                p.Nom           = (String)row["Nom"];
                p.Prenom        = (String)row["Prenom"];
                p.Salaire       = row["Salaire"] != DBNull.Value ? (float)(decimal)row["Salaire"] : 0;
                p.DateNaissance = (DateTime?)(row["DateNaissance"] == DBNull.Value ? null : row["DateNaissance"]);
                peuple.Add(p);
            }
            return(peuple);
        }
Example #9
0
        public bool addOrUpdatePersonne(MyPersonne personne)
        {
            MyPersonne person;

            if ((person = getPersonneById(personne.Id)) == null)
            {
                try
                {
                    using (SqlConnection cnx = new SqlConnection(ittacours))
                    {
                        cnx.Open();
                        string       sql     = @"Insert dbo.Personne ([Nom] ,[Prenom], [Salaire], [DateNaissance])
                                       values ( @nom, @prenom, @salaire, @datenaissance)";
                        SqlCommand   command = new SqlCommand(sql, cnx);
                        SqlParameter sp      = new SqlParameter("@nom", personne.Nom);
                        SqlParameter sp1     = new SqlParameter("@prenom", personne.Prenom);
                        SqlParameter sp2     = new SqlParameter("@salaire", personne.Salaire);
                        sp2.DbType = System.Data.DbType.Double;

                        SqlParameter sp3 = new SqlParameter("@datenaissance", (object)personne.DateNaissance ?? DBNull.Value);
                        command.Parameters.Add(sp);
                        command.Parameters.Add(sp1);
                        command.Parameters.Add(sp2);
                        command.Parameters.Add(sp3);
                        return(command.ExecuteNonQuery() == 1);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("add pas potible", ex);
                }
            }
            else
            {
                return(Update(personne));
            }
        }