private void buttonDeleteTable_Click(object sender, EventArgs e)
        {
            DataBase mData  = new DataBase("prime_db");
            DataBase userDB = new DataBase(currentDB);

            mData.OpenConnection();
            bool flag = false;

            try
            {
                userDB.OpenConnection();
                MySqlCommand commandCreate = new MySqlCommand($"DROP TABLE IF EXISTS {comboBoxTables.Text}", userDB.GetConnection());
                commandCreate.ExecuteNonQuery();
            }
            catch (MySqlException ex)
            {
                flag = true;
                MessageBox.Show($"Не получилось произвести удаление таблицы {comboBoxTables.Text}. Попробуйте сначала удалить зависимости");
            }

            if (!flag)
            {
                MySqlCommand commandIns = new MySqlCommand($"DELETE FROM `table` WHERE Name = \"{comboBoxTables.Text}\"", mData.GetConnection());
                commandIns.ExecuteNonQuery();
                MessageBox.Show($"Таблица {comboBoxTables.Text} успешно удалена!");
            }
            DBChangeForm form = new DBChangeForm(currentDB);

            form.Show();
            this.Hide();
        }
        private void DeleteAttrButton_Click(object sender, EventArgs e)
        {
            DataBase mData        = new DataBase("prime_db");
            DataBase userDB       = new DataBase(currentDB);
            string   deopAttrName = AllAttrsListBox.SelectedItem.ToString();
            string   sourceName   = GetTableName(GetSourceId(deopAttrName));
            bool     flag         = false;

            mData.OpenConnection();
            userDB.OpenConnection();

            try
            {
                MySqlCommand altCom = new MySqlCommand($"ALTER TABLE `{sourceName}` DROP COLUMN `{deopAttrName}`", userDB.GetConnection());
                altCom.ExecuteNonQuery();
            }catch (MySqlException ex)
            {
                flag = true;
                MessageBox.Show($"Не удалось произвести удаление атрибута {deopAttrName}");
            }

            if (!flag)
            {
                MySqlCommand com = new MySqlCommand($"DELETE FROM `attribute` WHERE Attribute_Name = \"{deopAttrName}\"", mData.GetConnection());
                com.ExecuteNonQuery();
                MessageBox.Show($"Удаление атрибута {deopAttrName} выполнено!");
                DBChangeForm form = new DBChangeForm(currentDB);
                form.Show();
                this.Hide();
            }
        }
        private void buttonBack_Click(object sender, EventArgs e)
        {
            DBChangeForm form = new DBChangeForm(currentDB, tableName);

            form.Show();
            this.Hide();
        }
        private void buttonChangeDB_Click(object sender, EventArgs e)
        {
            DBChangeForm form = new DBChangeForm(currentDB);

            form.Show();
            this.Hide();
        }
示例#5
0
        private void buttonCreateTable_Click(object sender, EventArgs e)
        {
            DataBase mData  = new DataBase("prime_db");
            DataBase userDB = new DataBase(currentDB);

            mData.OpenConnection();
            DataTable table = GetTable(mData);
            int       nextID;

            if (table.Rows.Count > 0)
            {
                nextID = UnicID(table);
            }
            else
            {
                nextID = 1;
            }
            MySqlCommand commandIns = new MySqlCommand($"INSERT INTO `table` (Table_ID,Name,Description) VALUES ({nextID},\"{tableName}\",\"{richTextBox1.Text}\")", mData.GetConnection());

            commandIns.ExecuteNonQuery();

            userDB.OpenConnection();
            MySqlCommand commandCreate = new MySqlCommand($@"
                    CREATE TABLE {tableName}
                    (I_D INT NOT NULL)", userDB.GetConnection());

            commandCreate.ExecuteNonQuery();

            DBChangeForm form = new DBChangeForm(currentDB);

            form.Show();
            this.Hide();
            MessageBox.Show($"Таблица {tableName} успешно добавлена!");
        }
        private void DeleteConnectionButton_Click(object sender, EventArgs e)
        {
            DataBase mData    = new DataBase("prime_db");
            DataBase userDate = new DataBase(currentDB);

            string[] fkey         = ConnectionsListBox.SelectedItem.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            int      connID       = Convert.ToInt32(fkey[0]);
            string   connName     = GetConnectionName(connID);
            string   fKeyAttrName = fkey[1];
            string   depTName     = GetTableName(GetSourceId(fKeyAttrName));

            mData.OpenConnection();
            userDate.OpenConnection();

            bool flag = false;

            try
            {
                MySqlCommand com = new MySqlCommand($"ALTER TABLE `{depTName}` DROP FOREIGN KEY {connName}", userDate.GetConnection());
                com.ExecuteNonQuery();
            }catch (MySqlException ex)
            {
                flag = true;
                MessageBox.Show("Не получилось удалить связь");
            }

            if (!flag)
            {
                MySqlCommand remEntCom = new MySqlCommand($"DELETE FROM `connection` WHERE Connection_ID = {connID}", mData.GetConnection());
                remEntCom.ExecuteNonQuery();
                MessageBox.Show("Удаление внешнего ключа выполнено успешно!");
                DBChangeForm form = new DBChangeForm(currentDB);
                form.Show();
                this.Hide();
            }
        }
        private void CreateAttrButton_Click(object sender, EventArgs e)
        {
            DataBase mData  = new DataBase("prime_db");
            DataBase userDB = new DataBase(currentDB);
            int      tID    = GetTableID(tableName);

            int UniqueValue  = 0;
            int KeyValue     = 0;
            int CanNullValue = 0;

            if (UniqueCheckBox.Checked)
            {
                UniqueValue = 1;
            }
            if (CanNullCheckBox.Checked)
            {
                CanNullValue = 1;
            }

            string attrName = AttrNametextBox.Text;
            string type     = TypescomboBox.Text;
            string strValue = StartValueTextBox.Text;
            string desc     = richTextBox1.Text;

            #region CreateAttr
            bool         flag = false;
            MySqlCommand altCom;

            try
            {
                userDB.OpenConnection();
                string nullDef = "NULL";
                if (CanNullValue == 0)
                {
                    nullDef = "Not Null";
                }
                altCom = new MySqlCommand($"ALTER TABLE `{tableName}` ADD COLUMN {attrName} {type} {nullDef}", userDB.GetConnection());
                altCom.ExecuteNonQuery();

                if (UniqueValue == 1)
                {
                    altCom = new MySqlCommand($"ALTER TABLE `{tableName}` ADD CONSTRAINT {attrName}_un_const UNIQUE ({attrName})", userDB.GetConnection());
                    altCom.ExecuteNonQuery();
                }
            }catch (MySqlException)
            {
                flag = true;
                MessageBox.Show("Не получилось добавить новый атрибут");
            }

            try
            {
                altCom = new MySqlCommand($"ALTER TABLE `{tableName}` DROP COLUMN `I_D`", userDB.GetConnection());
                altCom.ExecuteNonQuery();
            }
            catch (MySqlException ex)
            {
                //Игнорируем, если удаление уже производилось
            }
            #endregion

            if (!flag)
            {
                #region InsertIntoPrime

                mData.OpenConnection();
                DataTable table = GetTable(mData);
                int       nextID;
                if (table.Rows.Count > 0)
                {
                    nextID = UnicID(table);
                }
                else
                {
                    nextID = 1;
                }

                MySqlCommand commandIns = new MySqlCommand($"INSERT INTO `attribute` (Attribute_ID,Attribute_Name,Type,Start_Value,Is_Unique,Is_Key,Can_Null,Description,Table_ID) VALUES ({nextID},\"{attrName}\",\"{type}\",\"{strValue}\",{UniqueValue},{KeyValue},{CanNullValue},\"{desc}\",{tID})", mData.GetConnection());
                commandIns.ExecuteNonQuery();
                mData.CloseConnection();
                #endregion



                if (strValue != "")
                {
                    try
                    {
                        userDB.OpenConnection();
                        switch (type)
                        {
                        case ("Int"):
                        {
                            MySqlCommand defCom = new MySqlCommand($"ALTER TABLE `{tableName}` ALTER COLUMN `{attrName}` SET DEFAULT {strValue} ", userDB.GetConnection());
                            defCom.ExecuteNonQuery();
                            break;
                        }

                        default:
                        {
                            MySqlCommand defCom = new MySqlCommand($"ALTER TABLE `{tableName}` ALTER COLUMN `{attrName}` SET DEFAULT '{strValue}' ", userDB.GetConnection());
                            defCom.ExecuteNonQuery();
                            break;
                        }
                        }
                    }
                    catch (MySqlException ex)
                    {
                        MessageBox.Show($"Значение по умолчанию введено некорректно. Атрибут {attrName} добавлен без значения по умолчанию.");
                        DBChangeForm tform = new DBChangeForm(currentDB, tableName);
                        tform.Show();
                        this.Hide();
                        return;
                    }
                }

                DBChangeForm form = new DBChangeForm(currentDB, tableName);
                form.Show();
                this.Hide();
                MessageBox.Show($"Атрибут {attrName} успешно добавлен!");
            }
            else
            {
                DBChangeForm form = new DBChangeForm(currentDB, tableName);
                form.Show();
                this.Hide();
            }
        }