private void сохранитьToolStripMenuItem_Click(object sender, EventArgs e) { var rows = dataGridView1.Rows; for (int i = 0; i < rows.Count; i++) { var nameCell = rows[i].Cells[0]; var attr = (Attribute)nameCell.Tag; attr.Name = nameCell.EditedFormattedValue.ToString(); } metaDbContainer.SaveChanges(); MessageBox.Show("Изменения успешно сохранены"); Close(); }
private void button1_Click(object sender, EventArgs e) { if (nameTextBox.Text.Length == 0) { MessageBox.Show("Укажите имя атрибута"); return; } if (typeComboBox.SelectedIndex < 0) { MessageBox.Show("Укажите выберите тип атрибута"); return; } int sqlResCode = SqlExecutor.ExecuteNonQuery(dbConnection, $"ALTER TABLE {currentTable.RealName} ADD col{currentTable.Attributes.Count} {MapType(typeComboBox.Text)} {((!isNullableCheckBox.Checked) ? "NOT" : "")} NULL"); bool itIsFK = typeComboBox.Text.StartsWith("Ссылка на"); if (sqlResCode > -1 && itIsFK) { string realName = tableRealName2Name[typeComboBox.Text]; string pkName = metaDbContainer.TableSet.Where(t => t.RealName == realName).First().Attributes .Where(a => a.IsPKey).First().RealName; sqlResCode = SqlExecutor.ExecuteNonQuery(dbConnection, $"ALTER TABLE {currentTable.RealName} ADD CONSTRAINT FK_{currentTable.RealName}_col{currentTable.Attributes.Count} FOREIGN KEY (col{currentTable.Attributes.Count}) REFERENCES {realName}({pkName})"); } if (sqlResCode < 0) { return; } currentTable.Attributes.Add(new Model.Attribute( name: nameTextBox.Text, realname: $"col{currentTable.Attributes.Count}", type: MapAttrType(typeComboBox.Text), isIndexed: (itIsFK) ? true : isIndexedCheckBox.Checked, isNullable: isNullableCheckBox.Checked, isPKey: false )); metaDbContainer.SaveChanges(); if (itIsFK) { var prn = tableRealName2Name[typeComboBox.Text]; var parent = metaDbContainer.TableSet.First(t => t.RealName == prn); currentTable.ParentTables.Add(parent); parent.ChildTables.Add(currentTable); } MessageBox.Show("Атрибут успешно добавлен"); this.DialogResult = DialogResult.OK; this.Close(); }
private void QueriesForm_FormClosing(object sender, FormClosingEventArgs e) { var result = MessageBox.Show("Сохранить?", "Закрытие формы", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == DialogResult.Cancel) { e.Cancel = true; return; } if (result == DialogResult.Yes) { Query query = new Model.Query(this.QueryName); //add selected attr + joins var selectedAttributes = listBoxSelected.Items.Cast <string>().ToArray(); var joinedTables = listBoxJoins.Items.Cast <string>().ToArray(); query.QueryText = "" + string.Join(",", selectedAttributes) + "|" + string.Join(",", joinedTables); //add where clause query.QueryText += "|" + cmbItems1.SelectedIndex + "," + cmbOperations1.SelectedIndex + "," + txbValues1.Text + "," + cmbJoins1.SelectedIndex; query.QueryText += "|" + cmbItems2.SelectedIndex + "," + cmbOperations2.SelectedIndex + "," + txbValues2.Text + "," + cmbJoins2.SelectedIndex; query.QueryText += "|" + cmbItems3.SelectedIndex + "," + cmbOperations3.SelectedIndex + "," + txbValues3.Text + ","; if (needToAdd) { metaDbContainer.QuerySet.Add(query); metaDbContainer.SaveChanges(); } else { var oldQuery = metaDbContainer.QuerySet.Where(x => x.Name == query.Name).FirstOrDefault(); metaDbContainer.QuerySet.Remove(oldQuery); metaDbContainer.QuerySet.Add(query); metaDbContainer.SaveChanges(); } } ((MainForm)Parent.Parent).LoadTreeView(); }
void ViewForm_FormClosing(object sender, FormClosingEventArgs e) { var result = MessageBox.Show("Сохранить?", "Закрытие формы", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == DialogResult.Cancel) { e.Cancel = true; return; } if (result == DialogResult.Yes) { SaveViewForm(); _cont.SaveChanges(); } ((MainForm)Parent.Parent).LoadTreeView(); }
public ViewForm() { InitializeComponent(); try { _cont = new MetaDataDBContainer(); view = new Model.View("NewView"); _cont.ViewSet.Add(view); _cont.SaveChanges(); InitializeAttributePage(); } catch (Exception e) { MessageBox.Show("Возникла ошибка"); _log.Error(e); //SaveError(e); } }
public ViewForm(MetaDataDBContainer Container, SqlConnection dbConnection, string ViewName) { InitializeComponent(); try { _dbConnection = dbConnection; _cont = Container; view = new Model.View(ViewName); _cont.ViewSet.Add(view); _cont.SaveChanges(); InitializeAttributePage(); } catch (Exception e) { MessageBox.Show("Возникла ошибка"); _log.Error(e); //SaveError(e); } }
private void переименоватьToolStripMenuItem_Click(object sender, EventArgs e) { ToolStrip ts = ((ToolStripMenuItem)sender).GetCurrentParent(); string name = ts.Tag.ToString().Substring(6); TreeNode node = MainTreeView.Nodes.Cast <TreeNode>() .SelectMany(x => x.Nodes.Cast <TreeNode>()) .First(t => t.Tag.Equals($"{ts.Tag}")); GetStringForm gst = new GetStringForm("Введите новое имя", "Переименование"); gst.SetValue(node.Text); if (gst.ShowDialog() == DialogResult.Cancel || gst.Value == node.Text) { return; } string newName = gst.Value; switch (ts.Tag.ToString().Substring(0, 6)) { case "[tabl]": { if (metaDbContainer.TableSet.Any(x => x.Name == newName)) { MessageBox.Show("Таблица с таким именем уже существует", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); переименоватьToolStripMenuItem_Click(sender, null); } var t = metaDbContainer.TableSet.First(x => x.RealName == name); t.Name = newName; metaDbContainer.Entry(t).State = EntityState.Modified; metaDbContainer.SaveChanges(); break; } case "[quer]": { if (metaDbContainer.QuerySet.Any(x => x.Name == newName)) { MessageBox.Show("Запрос с таким именем уже существует", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); переименоватьToolStripMenuItem_Click(sender, null); } node.Tag = node.Tag.ToString().Substring(0, 6) + newName; var q = metaDbContainer.QuerySet.First(x => x.Name == name); q.Name = newName; metaDbContainer.Entry(q).State = EntityState.Modified; metaDbContainer.SaveChanges(); break; } case "[view]": { if (metaDbContainer.ViewSet.Any(x => x.Name == newName)) { MessageBox.Show("Форма с таким именем уже существует", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); переименоватьToolStripMenuItem_Click(sender, null); } var v = metaDbContainer.ViewSet.First(x => x.Name == name); v.Name = newName; metaDbContainer.Entry(v).State = EntityState.Modified; metaDbContainer.SaveChanges(); node.Tag = node.Tag.ToString().Substring(0, 6) + newName; break; } case "[repo]": { if (metaDbContainer.ReportSet.Any(x => x.Name == newName)) { MessageBox.Show("Отчёт с таким именем уже существует", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); переименоватьToolStripMenuItem_Click(sender, null); } var r = metaDbContainer.ReportSet.First(x => x.Name == name); r.Name = newName; metaDbContainer.Entry(r).State = EntityState.Modified; metaDbContainer.SaveChanges(); break; } } node.Text = newName; }
private void создатьToolStripMenuItem_Click(object sender, EventArgs e) { DataGridViewRowCollection attributes = dataGridView.Rows; System.Collections.IList list = attributes; var tableName = tableNameTextBox.Text; var realTableName = "t" + DateTime.Now.ToString("ddMMyyyy_HHmmss"); if (!CheckValidity()) { return; } var dataGridViewRows = attributes.Cast <DataGridViewRow>() .Where(row => (string)row.Cells[0].EditedFormattedValue != ""); var createCols = dataGridViewRows .Select((row, index) => Row2SqlCreateTable(row, index)); var fkStartIndex = dataGridViewRows.Count(); var fkAttributes = fkDataGridView.Rows.Cast <DataGridViewRow>().Where(row => (string)row.Cells[0].EditedFormattedValue != ""); var fkCols = fkAttributes.Select((row, index) => FKRow2SqlCreateTable(row, index + fkStartIndex)); var colsInTable = createCols.Union(fkCols).ToList(); // create table sqlExecutor.ExecuteNonQuery($"CREATE TABLE {realTableName}({string.Join(", ", colsInTable)})"); // create indexes dataGridViewRows .Select((row, index) => Row2SqlCreateIndex(row, index, realTableName)) .Where(x => x != "") .Select(sqlExecutor.ExecuteNonQuery).ToList(); // create PK dataGridViewRows .Select((row, index) => Row2SqlPK(row, index, realTableName)) .Where(x => x != "") .Select(sqlExecutor.ExecuteNonQuery).ToList(); // add constraint on FK for current table if (fkAttributes.Count() > 0) { sqlExecutor.ExecuteNonQuery($"ALTER TABLE {realTableName} ADD " + string.Join(", ", fkAttributes.Select((row, index) => Row2FKSql(row, index + fkStartIndex, realTableName))) ); } // create EF Table var efTable = new Table(tableName, realTableName); efTable.Attributes = dataGridViewRows .Select((row, index) => Row2Attribute(row, index)) .Union( fkAttributes.Select((row, index) => Row2FKAttribute(row, index + fkStartIndex)) ) .ToList(); efTable.ParentTables = fkAttributes.Select(row => Row2ParentTable(row)).ToList(); metaDbContainer.TableSet.Add(efTable); metaDbContainer.SaveChanges(); fkAttributes.Select(row => { string tn = row.Cells[0].EditedFormattedValue.ToString(); string realName = tableRealName2Name[tn]; metaDbContainer.TableSet.Where(x => x.RealName == realName).First().ChildTables.Add(efTable); return(0); }).ToList(); metaDbContainer.SaveChanges(); log.Info($"Table {tableName} ({realTableName}) was created successfully"); MessageBox.Show("Таблица успешно создана!"); ((MainForm)Parent.Parent).LoadTreeView(); this.Close(); }