示例#1
0
        public static void CreateList()
        {
            if (connStr != null)
            {
                OleDbConnection con = new OleDbConnection(connStr);
                using (con)
                {
                    con.Open();

                    OleDbCommand cmd = new OleDbCommand();
                    cmd.Connection = con;

                    try
                    {
                        OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Veicoli", con);
                        DataTable        table   = new DataTable();
                        adapter.Fill(table);

                        foreach (DataRow item in table.Rows)
                        {
                            if (int.TryParse(item.ItemArray[10].ToString(), out _))
                            {
                                Auto a = new Auto(item.ItemArray[0].ToString(), item.ItemArray[1].ToString(), item.ItemArray[2].ToString(), item.ItemArray[3].ToString(), Convert.ToInt32(item.ItemArray[4]), Convert.ToDouble(item.ItemArray[5]),
                                                  Convert.ToDateTime(item.ItemArray[6]), Convert.ToBoolean(item.ItemArray[7]), Convert.ToBoolean(item.ItemArray[8]), Convert.ToInt32(item.ItemArray[9]),
                                                  Convert.ToInt32(item.ItemArray[10]));
                                bindingListVeicoli.Add(a);
                            }
                            else
                            {
                                Moto m = new Moto(item.ItemArray[0].ToString(), item.ItemArray[1].ToString(), item.ItemArray[2].ToString(), item.ItemArray[3].ToString(), Convert.ToInt32(item.ItemArray[4]), Convert.ToDouble(item.ItemArray[5]),
                                                  Convert.ToDateTime(item.ItemArray[6]), Convert.ToBoolean(item.ItemArray[7]), Convert.ToBoolean(item.ItemArray[8]), Convert.ToInt32(item.ItemArray[9]),
                                                  item.ItemArray[10].ToString());
                                bindingListVeicoli.Add(m);
                            }
                        }
                    }
                    catch (OleDbException exc)
                    {
                        Console.WriteLine("\n " + exc.Message);
                        System.Threading.Thread.Sleep(4000);
                        return;
                    }
                }
            }
        }
        public static void DeserializeJson(SerializableBindingList <Veicolo> lista, string fileName)
        {
            lista.Clear();
            string json = File.ReadAllText(fileName);

            object[] veicoli = JsonConvert.DeserializeObject <object[]>(json);
            for (int i = 0; i < veicoli.Length; i++)
            {
                Moto   moto    = new Moto();
                Auto   auto    = new Auto();
                string veicolo = veicoli[i].ToString();
                if (veicolo.Contains("MarcaSella"))
                {
                    JsonConvert.PopulateObject(veicolo, moto);
                    lista.Add(moto);
                }
                else
                {
                    JsonConvert.PopulateObject(veicolo, auto);
                    lista.Add(auto);
                }
            }
        }