Ejemplo n.º 1
0
        public void AddGumtreeFiles()
        {
            listView2.Clear();

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter      = "HTML (*.html)|*.html|" + "All files (*.*)|*.*";
            ofd.Multiselect = true;
            ofd.Title       = "HTML file browser";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                foreach (string file in ofd.FileNames)
                {
                    string fullHtml = File.ReadAllText(file);
                    SqlCar newCar   = WebScraper.NewGumtreeCar(fullHtml);
                    pendingAddCars.Add(newCar);

                    ListViewItem newItem = new ListViewItem(newCar.id.ToString());
                    foreach (PropertyInfo prop in newCar.GetType().GetProperties())
                    {
                        if (prop.Name != "id")
                        {
                            newItem.SubItems.Add(prop.GetValue(newCar, null).ToString());
                        }
                    }
                    listView2.Items.Add(newItem);
                }
            }
        }
Ejemplo n.º 2
0
        // Add one car via manual method (but I am still going to add cars en masse with this method because I want to wrap this project up)
        public static async Task CreateDatabaseEntryAsync(SqlCar newCar)
        {
            List <string> columns = GetColumnNames();

            columns.RemoveAt(0); // Remove id column as it is assigned automatically as opposed to if (str != "Id")
            string cmdStrMiddle = String.Join(",", columns.ToArray());

            cmdStrMiddle.TrimEnd(',');

            List <string> carPropertiesNames = new List <string>();

            foreach (PropertyInfo prop in newCar.GetType().GetProperties())
            {
                if (prop.Name != "id")
                {
                    carPropertiesNames.Add("@_" + prop.Name);
                }
            }
            string cmdStrEnd = String.Join(",", carPropertiesNames.ToArray());

            cmdStrEnd.TrimEnd(',');

            await connect.OpenAsync();

            string     commandString = String.Format("INSERT INTO Car (" + cmdStrMiddle + ") VALUES (" + cmdStrEnd + ")");
            SqlCommand command       = new SqlCommand(commandString, connect);

            foreach (PropertyInfo prop in newCar.GetType().GetProperties())
            {
                if (prop.Name != "id")
                {
                    command.Parameters.AddWithValue("@_" + prop.Name, prop.GetValue(newCar, null));
                }
            }

            await command.ExecuteNonQueryAsync();

            connect.Close();
        }