Beispiel #1
0
        private void AddTableButton_Click(object sender, System.EventArgs e)
        {
            Database db;
            Table tbl;
            Column col;
            Index idx;
            Default dflt;
            Cursor csr = null;

            try
            {
                csr = this.Cursor;   // Save the old cursor
                this.Cursor = Cursors.WaitCursor;   // Display the waiting cursor

                // Show the current tables for the selected database
                db = (Database)DatabasesComboBox.SelectedItem;
                if (db.Tables.Contains(TableNameTextBox.Text) == false)
                {
                    // Create an empty string default
                    dflt = db.Defaults["dfltEmptyString"];
                    if (dflt == null)
                    {
                        dflt = new Default(db, "dfltEmptyString");
                        dflt.TextHeader = "CREATE DEFAULT [dbo].[dfltEmptyString] AS ";
                        dflt.TextBody = @"'';";
                        dflt.Create();
                    }

                    // Create a new table object
                    tbl = new Table(db,
                        TableNameTextBox.Text, db.DefaultSchema);

                    // Add the first column
                    col = new Column(tbl, @"Column1", DataType.Int);
                    tbl.Columns.Add(col);
                    col.Nullable = false;
                    col.Identity = true;
                    col.IdentitySeed = 1;
                    col.IdentityIncrement = 1;

                    // Add the primary key index
                    idx = new Index(tbl, @"PK_" + TableNameTextBox.Text);
                    tbl.Indexes.Add(idx);
                    idx.IndexedColumns.Add(new IndexedColumn(idx, col.Name));
                    idx.IsClustered = true;
                    idx.IsUnique = true;
                    idx.IndexKeyType = IndexKeyType.DriPrimaryKey;

                    // Add the second column
                    col = new Column(tbl, @"Column2", DataType.NVarChar(1024));
                    tbl.Columns.Add(col);
                    col.DataType.MaximumLength = 1024;
                    col.AddDefaultConstraint(null); // Use SQL Server default naming
                    col.DefaultConstraint.Text = Properties.Resources.DefaultConstraintText;
                    col.Nullable = false;

                    // Add the third column
                    col = new Column(tbl, @"Column3", DataType.DateTime);
                    tbl.Columns.Add(col);
                    col.Nullable = false;

                    // Create the table
                    tbl.Create();

                    // Refresh list and select the one we just created
                    ShowTables();

                    // Clear selected items
                    TablesComboBox.SelectedIndex = -1;

                    // Find the table just created
                    TablesComboBox.SelectedIndex = TablesComboBox.FindStringExact(tbl.ToString());
                }
                else
                {
                    ExceptionMessageBox emb = new ExceptionMessageBox();
                    emb.Text = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                        Properties.Resources.TableExists, TableNameTextBox.Text);
                    emb.Show(this);
                }
            }
            catch (SmoException ex)
            {
                ExceptionMessageBox emb = new ExceptionMessageBox(ex);
                emb.Show(this);
            }
            finally
            {
                this.Cursor = csr;  // Restore the original cursor
            }
        }
Beispiel #2
0
        private void AddTableButton_Click(object sender, System.EventArgs e)
        {
            Database db;
            Table    tbl;
            Column   col;
            Index    idx;
            Default  dflt;
            Cursor   csr = null;

            try
            {
                csr         = this.Cursor;        // Save the old cursor
                this.Cursor = Cursors.WaitCursor; // Display the waiting cursor

                // Show the current tables for the selected database
                db = (Database)DatabasesComboBox.SelectedItem;
                if (db.Tables.Contains(TableNameTextBox.Text) == false)
                {
                    // Create an empty string default
                    dflt = db.Defaults["dfltEmptyString"];
                    if (dflt == null)
                    {
                        dflt            = new Default(db, "dfltEmptyString");
                        dflt.TextHeader = "CREATE DEFAULT [dbo].[dfltEmptyString] AS ";
                        dflt.TextBody   = @"'';";
                        dflt.Create();
                    }

                    // Create a new table object
                    tbl = new Table(db,
                                    TableNameTextBox.Text, db.DefaultSchema);

                    // Add the first column
                    col = new Column(tbl, @"Column1", DataType.Int);
                    tbl.Columns.Add(col);
                    col.Nullable          = false;
                    col.Identity          = true;
                    col.IdentitySeed      = 1;
                    col.IdentityIncrement = 1;

                    // Add the primary key index
                    idx = new Index(tbl, @"PK_" + TableNameTextBox.Text);
                    tbl.Indexes.Add(idx);
                    idx.IndexedColumns.Add(new IndexedColumn(idx, col.Name));
                    idx.IsClustered  = true;
                    idx.IsUnique     = true;
                    idx.IndexKeyType = IndexKeyType.DriPrimaryKey;

                    // Add the second column
                    col = new Column(tbl, @"Column2", DataType.NVarChar(1024));
                    tbl.Columns.Add(col);
                    col.DataType.MaximumLength = 1024;
                    col.AddDefaultConstraint(null); // Use SQL Server default naming
                    col.DefaultConstraint.Text = Properties.Resources.DefaultConstraintText;
                    col.Nullable = false;

                    // Add the third column
                    col = new Column(tbl, @"Column3", DataType.DateTime);
                    tbl.Columns.Add(col);
                    col.Nullable = false;

                    // Create the table
                    tbl.Create();

                    // Refresh list and select the one we just created
                    ShowTables();

                    // Clear selected items
                    TablesComboBox.SelectedIndex = -1;

                    // Find the table just created
                    TablesComboBox.SelectedIndex = TablesComboBox.FindStringExact(tbl.ToString());
                }
                else
                {
                    ExceptionMessageBox emb = new ExceptionMessageBox();
                    emb.Text = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                             Properties.Resources.TableExists, TableNameTextBox.Text);
                    emb.Show(this);
                }
            }
            catch (SmoException ex)
            {
                ExceptionMessageBox emb = new ExceptionMessageBox(ex);
                emb.Show(this);
            }
            finally
            {
                this.Cursor = csr;  // Restore the original cursor
            }
        }