Esempio n. 1
0
 public CrossTableFullRow(TableModelRow model, TableAirplaneRow airplane, TableSeatRow seat, TableAirportRow airport_in, TableAirportRow airport_out, TableFlightRow flight, TablePassengerRow passenger)
 {
     this.model       = model;
     this.airplane    = airplane;
     this.seat        = seat;
     this.airport_in  = airport_in;
     this.airport_out = airport_out;
     this.flight      = flight;
     this.passenger   = passenger;
 }
Esempio n. 2
0
        public ParserRow(TableModelRow model, TableAirplaneRow airplane, TableSeatRow seat, TableAirportRow airport_in, TableAirportRow airport_out, TableFlightRow flight, TablePassengerRow passenger)
        {
            tableRows = new CrossTableFullRow(
                model,
                airplane,
                seat,
                airport_in,
                airport_out,
                flight,
                passenger);

            fullRow = new FullRow(
                tableRows.model.name,
                tableRows.airplane.number,
                tableRows.seat.code,
                tableRows.seat.id,
                tableRows.airport_in.code,
                tableRows.airport_in.city,
                tableRows.airport_in.country,
                tableRows.airport_out.code,
                tableRows.airport_out.city,
                tableRows.airport_out.country,
                tableRows.flight.number,
                tableRows.flight.date_time,
                tableRows.passenger.number);
        }
Esempio n. 3
0
        static public void WritePostgreSQL(ParserTable tables)
        {
            //Удаление предыдущей базы данных (проще, чем чистить все данные)
            string connectionStringDefault = "Server=127.0.0.1;Port=5432;User Id=postgres;Password=masterkey;Database=postgres;";
            string queryCreateDB           =
                @"CREATE DATABASE outputdatabase
                WITH
                OWNER = postgres
                ENCODING = 'UTF8'
                LC_COLLATE = 'Russian_Russia.1251'
                LC_CTYPE = 'Russian_Russia.1251'
                TABLESPACE = pg_default
                CONNECTION LIMIT = -1;";
            int sucsess = 0;

            while (sucsess != 1)
            {
                Console.WriteLine("Подключение к базе PostgreSQL...");
                using (NpgsqlConnection connection = new NpgsqlConnection(connectionStringDefault))
                    using (NpgsqlCommand command = new NpgsqlCommand("DROP DATABASE outputdatabase;", connection))
                    {
                        try
                        {
                            connection.Open();

                            command.ExecuteNonQuery();
                            Console.WriteLine("Старые данные в базе PostgreSQL стерты");
                            connection.Close();
                            sucsess = 1;
                        }
                        catch (Exception ex)
                        {
                            connection.Close();
                            if (ex.Data["SqlState"].Equals("55006"))
                            {
                                sucsess--;
                                if (sucsess > -3)
                                {
                                    Console.WriteLine("Пожалуйста, закройте базу данных и нажмите любую клавишу для подтверждения...");
                                    Console.ReadKey(true);
                                }
                                else
                                {
                                    Console.WriteLine("База данных открыта в данный момент. Приложение будет закрыто.");
                                    Console.WriteLine("Нажмите любую клавишу для подтверждения...");
                                    Console.ReadKey(true);
                                    Environment.Exit(0);
                                }
                            }
                            else if (ex.Data["SqlState"].Equals("3D000"))
                            {
                                //ничего, такой базы данных не существует. Этого я и добивалась
                                sucsess = 1;
                            }
                            else if (ex.Data["SqlState"].Equals("28P01"))
                            {
                                Console.WriteLine("Неправильно указан логин или пароль. Приложение будет закрыто.");
                                Console.WriteLine("Нажмите любую клавишу для подтверждения...");
                                Console.ReadKey(true);
                                Environment.Exit(0);
                            }
                            else
                            {
                                Console.WriteLine("Произошла ошибка {0}. Приложение будет закрыто.", ex.Message);
                                Console.WriteLine("Нажмите любую клавишу для подтверждения...");
                                Console.ReadKey(true);
                                Environment.Exit(0);
                            }
                        }
                    }
            }

            //Создание БД заново
            try
            {
                using (NpgsqlConnection connection = new NpgsqlConnection(connectionStringDefault))
                    using (NpgsqlCommand command = new NpgsqlCommand(queryCreateDB, connection))
                    {
                        connection.Open();

                        command.ExecuteNonQuery();

                        connection.Close();

                        Console.WriteLine("Создана новая БД PostgreSQL");
                    }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Ошибка при создании БД PostgreSQL " + ex.Message);
                Console.WriteLine("Приложение будет закрыто.");
                Console.WriteLine("Нажмите любую клавишу для подтверждения...");
                Console.ReadKey(true);
                Environment.Exit(0);
            }

            //Наполнение БД
            string connectionString = "Server=127.0.0.1;Port=5432;User Id=postgres;Password=masterkey;Database=outputdatabase;";

            try
            {
                using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
                {
                    connection.Open();

                    //Создание таблиц
                    using (NpgsqlCommand command = new NpgsqlCommand(TableModelRow.GetCreateQuery(), connection))
                    {
                        try { command.ExecuteNonQuery(); }
                        catch (Exception ex) { Console.WriteLine("Ошибка при создании таблицы Model \n" + ex.Message); }
                    }
                    using (NpgsqlCommand command = new NpgsqlCommand(TableAirplaneRow.GetCreateQuery(), connection))
                    {
                        try { command.ExecuteNonQuery(); }
                        catch (Exception ex) { Console.WriteLine("Ошибка при создании таблицы Airplane \n" + ex.Message); }
                    }
                    using (NpgsqlCommand command = new NpgsqlCommand(TableAirportRow.GetCreateQuery(), connection))
                    {
                        try { command.ExecuteNonQuery(); }
                        catch (Exception ex) { Console.WriteLine("Ошибка при создании таблицы Airport \n" + ex.Message); }
                    }
                    using (NpgsqlCommand command = new NpgsqlCommand(TableSeatRow.GetCreateQuery(), connection))
                    {
                        try { command.ExecuteNonQuery(); }
                        catch (Exception ex) { Console.WriteLine("Ошибка при создании таблицы Seat \n" + ex.Message); }
                    }
                    using (NpgsqlCommand command = new NpgsqlCommand(TableFlightRow.GetCreateQuery(), connection))
                    {
                        try { command.ExecuteNonQuery(); }
                        catch (Exception ex) { Console.WriteLine("Ошибка при создании таблицы Flight \n" + ex.Message); }
                    }
                    using (NpgsqlCommand command = new NpgsqlCommand(TablePassengerRow.GetCreateQuery(), connection))
                    {
                        try { command.ExecuteNonQuery(); }
                        catch (Exception ex) { Console.WriteLine("Ошибка при создании таблицы Passenger \n" + ex.Message); }
                    }
                    Console.WriteLine("Таблицы БД PostgreSQL успешно созданы");

                    //Заполнение БД данными
                    List <TableModelRow>     tableModel     = tables.tableModel;
                    List <TableAirplaneRow>  tableAirplane  = tables.tableAirplane;
                    List <TableSeatRow>      tableSeat      = tables.tableSeat;
                    List <TableAirportRow>   tableAirport   = tables.tableAirport;
                    List <TableFlightRow>    tableFlight    = tables.tableFlight;
                    List <TablePassengerRow> tablePassenger = tables.tablePassenger;

                    int count = 0;
                    foreach (TableModelRow row in tableModel)
                    {
                        using (NpgsqlCommand command = new NpgsqlCommand(row.GetSQL(), connection))
                        {
                            try { command.ExecuteNonQuery(); count++; }
                            catch (Exception ex) { Console.WriteLine("Ошибка при вставке в Model " + row.ToString() + "\n" + ex.Message); }
                        }
                    }
                    Console.WriteLine("В таблицу Model добавлено {0} записей", count);

                    count = 0;
                    foreach (TableAirplaneRow row in tableAirplane)
                    {
                        using (NpgsqlCommand command = new NpgsqlCommand(row.GetSQL(), connection))
                        {
                            try { command.ExecuteNonQuery(); count++; }
                            catch (Exception ex) { Console.WriteLine("Ошибка при вставке в Airplane " + row.ToString() + "\n" + ex.Message); }
                        }
                    }
                    Console.WriteLine("В таблицу Airplane добавлено {0} записей", count);

                    count = 0;
                    foreach (TableSeatRow row in tableSeat)
                    {
                        using (NpgsqlCommand command = new NpgsqlCommand(row.GetSQL(), connection))
                        {
                            try { command.ExecuteNonQuery(); count++; }
                            catch (Exception ex) { Console.WriteLine("Ошибка при вставке в Seat " + row.ToString() + "\n" + ex.Message); }
                        }
                    }
                    Console.WriteLine("В таблицу Seat добавлено {0} записей", count);

                    count = 0;
                    foreach (TableAirportRow row in tableAirport)
                    {
                        using (NpgsqlCommand command = new NpgsqlCommand(row.GetSQL(), connection))
                        {
                            try { command.ExecuteNonQuery(); count++; }
                            catch (Exception ex) { Console.WriteLine("Ошибка при вставке в Airport " + row.ToString() + "\n" + ex.Message); }
                        }
                    }
                    Console.WriteLine("В таблицу Airport добавлено {0} записей", count);

                    count = 0;
                    foreach (TableFlightRow row in tableFlight)
                    {
                        using (NpgsqlCommand command = new NpgsqlCommand(row.GetSQL(), connection))
                        {
                            try { command.ExecuteNonQuery(); count++; }
                            catch (Exception ex) { Console.WriteLine("Ошибка при вставке в Flight " + row.ToString() + "\n" + ex.Message); }
                        }
                    }
                    Console.WriteLine("В таблицу Flight добавлено {0} записей", count);

                    count = 0;
                    foreach (TablePassengerRow row in tablePassenger)
                    {
                        using (NpgsqlCommand command = new NpgsqlCommand(row.GetSQL(), connection))
                        {
                            try { command.ExecuteNonQuery(); count++; }
                            catch (Exception ex) { Console.WriteLine("Ошибка при вставке в Passenger " + row.ToString() + "\n" + ex.Message); }
                        }
                    }
                    Console.WriteLine("В таблицу Passenger добавлено {0} записей", count);

                    Console.WriteLine("Все данные добавлены\n");
                    connection.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Ошибка при работе с БД PostgreSQL " + ex.Message);
                Console.WriteLine("Приложение будет закрыто.");
                Console.WriteLine("Нажмите любую клавишу для подтверждения...");
                Console.ReadKey(true);
                Environment.Exit(0);
            }
        }