Esempio n. 1
0
        public bool addNewATM(ATM newATM)
        {
            connect();

            string addressInsertCommand = $"INSERT INTO Addresses (street, house_num, city, zip_code, lat, lng) VALUES ('{newATM.address.street}', {newATM.address.house_num}, '{newATM.address.city}', '{newATM.address.zip_code}', {newATM.address.lat}, {newATM.address.lng})";
            string getLastInsert        = "SELECT LAST_INSERT_ID()";

            MySqlCommand getAddressID  = new MySqlCommand(getLastInsert);
            MySqlCommand addrsCommand  = new MySqlCommand(addressInsertCommand);
            MySqlCommand newAtmCommand = new MySqlCommand();

            getAddressID.CommandType = CommandType.Text;
            addrsCommand.CommandType = CommandType.Text;

            if (conn != null)
            {
                addrsCommand.Connection  = conn;
                getAddressID.Connection  = conn;
                newAtmCommand.Connection = conn;

                conn.Open();

                try {
                    // Executing the query in the SQL server
                    addrsCommand.ExecuteNonQuery();
                    int addrsID = int.Parse(getAddressID.ExecuteScalar().ToString());

                    string newAtmString = $"INSERT INTO Atms (capacity, live_money_avilable, atm_size, brand, Addresses_id) VALUES ('{newATM.capacity}', '{newATM.live_money_avilable}', '{newATM.size}', '{newATM.brand}', {addrsID})";
                    newAtmCommand.CommandText = newAtmString;
                    newAtmCommand.CommandType = CommandType.Text;

                    newAtmCommand.ExecuteNonQuery();
                } catch (MySqlException ex) {
                    throw ex;
                }

                // Closing SQL connection
                conn.Close();
                Console.WriteLine("new ATM has been created");

                return(true);
            }

            return(false);
        }