Ejemplo n.º 1
0
        private void salvataggioDatabase()
        {
            int[] idDb = new int[db.contaItem("Auto")];
            db.getIds("Auto", idDb);
            SerializableBindingList <int> idList = new SerializableBindingList <int>();

            foreach (Auto auto in bindingListVeicoli.OfType <Auto>())
            {
                if (auto.Id == 0)
                {
                    db.aggiungiRecord("Auto", auto.Marca, auto.Modello, auto.Colore, auto.Cilindrata, auto.PotenzaKw, auto.Immatricolazione,
                                      auto.IsUsato, auto.IsKmZero, auto.KmPercorsi, auto.Prezzo, auto.NumAirbag, "/");
                }
                else
                {
                    idList.Add(auto.Id);
                }
            }
            if (idList.Count != 0)
            {
                for (int i = 0; i < idDb.Length; i++)
                {
                    bool presente = false;
                    int  j        = 0;
                    do
                    {
                        if (idDb[i] == idList[j])
                        {
                            presente = true;
                        }
                        j++;
                    } while (j != idList.Count && presente == false);
                    if (presente == false)
                    {
                        db.eliminaRecord("Auto", idDb[i]);
                    }
                }
            }
            else
            {
                for (int i = 0; i < idDb.Length; i++)
                {
                    db.eliminaRecord("Auto", idDb[i]);
                }
            }

            idList.Clear();
            idDb = new int[db.contaItem("Moto")];
            db.getIds("Moto", idDb);
            foreach (Moto moto in bindingListVeicoli.OfType <Moto>())
            {
                if (moto.Id == 0)
                {
                    db.aggiungiRecord("Moto", moto.Marca, moto.Modello, moto.Colore, moto.Cilindrata, moto.PotenzaKw, moto.Immatricolazione,
                                      moto.IsUsato, moto.IsKmZero, moto.KmPercorsi, moto.Prezzo, 0, moto.MarcaSella);
                }
                else
                {
                    idList.Add(moto.Id);
                }
            }
            if (idList.Count != 0)
            {
                for (int i = 0; i < idList.Count; i++)
                {
                    bool presente = false;
                    int  j        = 0;
                    do
                    {
                        if (idList[i] == idDb[j])
                        {
                            presente = true;
                        }
                        j++;
                    } while (j != idDb.Length && presente == false);
                    if (presente == false)
                    {
                        db.eliminaRecord("Moto", idList[i]);
                    }
                }
            }
            else
            {
                for (int i = 0; i < idDb.Length; i++)
                {
                    db.eliminaRecord("Moto", idDb[i]);
                }
            }

            modificato = false;
            bindingListVeicoli.Clear();
            CaricaDati();
            MessageBox.Show("Dati salvati correttamente");
        }