示例#1
0
        public void AddOrUpdateColumn(string columnName, DataType type, int size, bool nullable = false)
        {
            if (Columns.Exists(c => c.Name.Equals(columnName)))
            {
                Column column = Columns.Find(c => c.Name.Equals(columnName));
                column.Type = type;
                column.Size = size;
                column.Nullable = nullable;
            }
            else
            {
                Column column = new Column();
                column.Name = columnName;
                column.Type = type;
                column.Size = size;
                column.Nullable = nullable;

                Columns.Add(column);
            }
        }
        private void SaveTableButton_Click(object sender, System.EventArgs e)
        {
            //TODO: validate inputs

            var tableName = TableNameTextBox.Text.ToString();
            if (string.IsNullOrEmpty(tableName))
            {
                MessageBox.Show(Resources.ResourceManager.GetString("MissingTableName"));
                return;
            }

            var result = database.Tables.Find(t => t.Name.Equals(tableName));
            if(result != null)
            {
                MessageBox.Show(Resources.ResourceManager.GetString("TableNameTaked"));
                return;
            }

            table = new Table();
            table.Name = tableName;

            try
            {
                for (int i = 0; i < TableDataGridView.Rows.Count - 1; i++)
                {

                    Column column = new Column();
                    column.Name = TableDataGridView.Rows[i].Cells[0].Value.ToString();
                    column.Type = DataTypeConverter.ToDataType(TableDataGridView.Rows[i].Cells[1].Value.ToString());
                    column.Size = Int32.Parse(TableDataGridView.Rows[i].Cells[2].Value.ToString());
                    bool isPrimaryKey = (bool) ((DataGridViewCheckBoxCell) TableDataGridView.Rows[i].Cells[3]).FormattedValue;
                    column.Unique = (bool) ((DataGridViewCheckBoxCell) TableDataGridView.Rows[i].Cells[4]).FormattedValue;
                    column.Nullable = (bool) ((DataGridViewCheckBoxCell)TableDataGridView.Rows[i].Cells[5]).FormattedValue;

                    if (isPrimaryKey)
                    {
                        table.PrimaryKey.Add(column.Name);
                    }

                    table.Columns.Add(column);
                }

                // Create indexes for unique values.
                foreach (var column in table.Columns.Where(c => c.Unique).Select(c => c.Name))
                {
                    Index index = new Index();
                    index.Name = string.Format("{0}_Index_{1}", table.Name, table.Indexes.Count + 1);
                    index.IndexMembers.Add(column);
                    index.Unique = true;
                    table.Indexes.Add(index);
                }

                if (database.GetTable(table.Name) != null)
                {
                    Table tableToDelete = database.GetTable(table.Name);
                    dbContext.Query.DeleteTable(database, tableToDelete);
                    database.Tables.Remove(tableToDelete);
                }

                database.Tables.Add(table);
            }
            catch (NullReferenceException exception)
            {
                MessageBox.Show("Complete all cells!\n" + exception.Message);
            }

            DialogResult = DialogResult.OK;
        }